• React训练营:基本元素的使用与状态管理


    前置条件

    1. node --version
    2. npm --version
    3. npx create-react-app react-project

    入门样例

    1.React中的状态
    2.React中的语法糖

    
    import React from 'react';
    import ReactDOM from 'react-dom';
    import './index.css';
    
    const rootNode = document.getElementById('root');
    
    function App() {
    	//在函数中的状态管理
      const [developer, setDeveloper] = React.useState({
        name:"",
        language:"python",
        yearsExperience:0,
        isEmployed:false
      });
      React.useEffect(()=>{
        document.title = developer.name;
      },[developer.name])
    
      function handleChangeLanguage(){
        setDeveloper({
          ...developer,
          language:"Javascript",
          yearsExperience:5
        });
    
      }
    
      function handleChangeYearsExperience(event){
        setDeveloper({
          ...developer,
          yearsExperience:event.target.value
        })
      }
    
      function handleToggleEmployment(){
        setDeveloper((prevState)=>({
          ...developer,
          isEmployed:!developer.isEmployed
        }))
      }
      function handleChangeName(event){
        setDeveloper({
          ...developer,
          name:event.target.value
        })
      }
      return (
        <div>
          <div><button onClick={handleChangeLanguage}>Change Language</button></div>
          <div><button onClick={handleToggleEmployment}>Change isEmployed</button></div>
          <div>{developer.language}</div>
          <div>
            <input type="number" onChange={handleChangeYearsExperience}></input>
            <p>I have {developer.yearsExperience} years of experience</p>
            <p>Employment status:{developer.isEmployed? "Employed":"UnEmployed"}</p>
            <input type="text"  placeholder="change name" onChange={handleChangeName}></input>
          </div>
        </div>
      )
    }
    ReactDOM.render(
      <div>
        <App/>
      </div>,
      rootNode
    );
    

    React中鼠标跟踪

    image

    1. 使用状态
    const [mousePostition,setMousePosition] = React.useState({ x:0,y:0});
    
    1. useEffect
    React.useEffect(()=>{
    	document.addEventListener('mousemove',handleMouseMove);
    },[]);
    
    1. 鼠标监听事件
      function handleMouseMove(event){
        setMousePosition({x:event.pageX, y:event.pageY});
      }
    
    1. 页面布局
    <p>
    	X :{mousePostition.x},Y:{mousePostition.y}
    </p>
    
    1. 离开页面时需要取消监听
    
      React.useEffect(()=>{
        document.addEventListener('mousemove',handleMouseMove);
    
        return ()=>{
          document.removeEventListener('mousemove',handleMouseMove);
        }
      },[]);
    
    

    Demo获取Github 中的个人信息

    import React from 'react';
    import ReactDOM from 'react-dom';
    import './index.css';
    
    
    
    const rootNode = document.getElementById('root');
    
    const baseUrl = 'https://api.github.com/users/';
    
    function App() {
      const [username,setUsername] = React.useState('rynerisraid')
      const [user,setUser] = React.useState(null);
    
      async function getUser(route){
        const response = await fetch(`${baseUrl}${route}`);
        const data = await response.json();
        setUser(data);
      }
      React.useEffect(()=>{
        getUser(username);
      },[]);
      return (
        <div>
          <input 
          type="text" 
          placeholder="Input username"
          onChange={event=>setUsername(event.target.value)}
          />
          <button onClick={event=>getUser(username)}>Search</button>
          <button>Clear</button>
          {user? (
          <div>
            <h2>User:{user.name}</h2>
            <p>{user.bio}</p>
            <img alt='avatar' src={user.avatar_url} style={{ height:50 }}></img>  
          </div>) : (<p>Loading</p>)}
          </div>
      )
    }
    
    
    ReactDOM.render(<App/>,rootNode);
    
  • 相关阅读:
    权限设计
    ts infer关键字
    Array初始化 以及 Array.prototype.map()的一些问题
    同步、异步、事件循环
    Spring学习笔记(一)
    【面试】关于get和post两种方法的不同。
    【算法】背包问题
    当你在浏览器输入一个网址(如http://www.taobao.com),按回车之后发生了什么?
    数据库语句复习笔记
    【算法】雀魂启动(笔试题)
  • 原文地址:https://www.cnblogs.com/rynerlute/p/16060661.html
Copyright © 2020-2023  润新知