useState
useState()传入的是默认值,返回一个数组,包含状态值和修改状态值的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import { useState } from "react"; function App() { const [count, setCount] = useState(0); function addCount() { setCount(count + 1); } return ( <div> {count} <button onClick={addCount}>+</button> </div> ); }
export default App;
|
useContext
多个组件共享状态。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import React, { useContext } from "react";
const AppContext = React.createContext();
function ComponentA() { const { name } = useContext(AppContext); return ( <> <div>我是真的{name}!</div> </> ); }
function ComponentB() { const { name } = useContext(AppContext); return <div>我才是真的{name}!!!5555...</div>; }
export default function App() { return ( <AppContext.Provider value={{ name: "小猪" }}> <ComponentA></ComponentA> <ComponentB></ComponentB> </AppContext.Provider> ); }
|
useReducer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| import React, { useReducer } from "react";
export default function App() { const reducer = (state, action) => { switch (action.type) { case "add": return { ...state, count: state.count + 1, }; default: return state; } };
const [state, dispath] = useReducer(reducer, { count: 0 }); const addCount = () => dispath({ type: "add" }); return ( <div> 我是组件一 {state.count} <button onClick={addCount}>+</button> </div> ); }
|
useContext和useReducer配合使用
useContext和useReducer一起使用可以达到redux的效果,基本思路如下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import React, { createContext,useReducer } from 'react';
export const ColorContext = createContext()
export const UPDATE_COLOR = "UPDATE_COLOR"
const reducer= (state,action)=>{ switch(action.type){ case UPDATE_COLOR: return action.color default: return state } }
export const Color = props=>{ const [color,dispatch]=useReducer(reducer,'blue') return ( <ColorContext.Provider value={{color,dispatch}}> {props.children} </ColorContext.Provider> ) }
|
使用Color组件包含住要共享状态的组件,同时将dispath方法一并共享,在组件内部就可以利用dispatch方法修改共享值。
useEffect
充当生命周期函数的作用,在组件第一次渲染的时候会执行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import React, { useState, useEffect } from "react"; export default function App() { const [loading, setLoading] = useState(true); useEffect(() => { console.log("组件渲染了"); });
useEffect(() => { console.log("组件销毁了"); }, []);
useEffect(() => { console.log("loading改变了"); }, [loading]); const changeLoading = () => { setLoading(!loading); }; return ( <> <button onClick={changeLoading}>+</button> </> ); }
|
useMemo
性能优化,父组件状态更新,子组件中的方法会重新执行,带来性能损耗。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import React, { useState, useMemo } from "react";
const Children = (props) => { function Name() { console.log("一个与父组件无关的子组件被调用"); return "我是" + props.name; } const getName = useMemo(() => Name(), [props.name]); return <div>{getName}</div>; };
export default function App() { const [pig, setPig] = useState("小猪"); const [dog, setDog] = useState("小狗"); return ( <div> <p>{pig}</p> <br /> <button onClick={() => setPig("小狗")}>变成小狗</button> <Children name={dog} /> </div> ); }
|
useRef
获取DOM结点用的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import React, { useState, useRef } from "react"; export default function App() { const [pig, setPig] = useState("小猪"); const [dog, setDog] = useState("小狗"); const dom = useRef(null); const OpRef = () => { console.log(dom); dom.current.value="你好啊,小猪猪" }; return ( <div> <input ref={dom} value={pig} readOnly></input> <li>{dog}</li> <button onClick={()=>OpRef()}>看看ref</button> </div> ); }
|