React 备忘单

React hooks 和组件的快速参考。

useState

const [state, setState] = useState(initialState);
Returns a stateful value and a function to update it.

useEffect

useEffect(() => { ... }, [dependencies]);
Perform side effects in function components.

useContext

const value = useContext(MyContext);
Accepts a context object and returns the current context value.

useRef

const refContainer = useRef(initialValue);
Persist values between renders, access DOM nodes.

useReducer

const [state, dispatch] = useReducer(reducer, initialArg, init);
An alternative to useState for complex state logic.

useMemo

const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);
Returns a memoized value.

useCallback

const memoizedCallback = useCallback(() => { doSomething(a, b); }, [a, b]);
Returns a memoized callback.

React.memo

const MyComponent = React.memo(function MyComponent(props) { ... });
HOC for component memoization.

useId

const id = useId();
A hook for generating unique IDs.

useTransition

const [isPending, startTransition] = useTransition();
Allows components to avoid blocking UI during state updates.