zoukankan      html  css  js  c++  java
  • [React] Always useMemo your context value

    Have a similar post about Reac.memo. This blog is the take away from this post.

    To understand why to use 'React.useMemo' or 'React.memo' (basiclly lastest React version use 'useMemo'), is that if the function component or context create a new reference, you need to use memo. 

    Context: 

    Context provider always create a new object to the comsuers. So use memo everytime you can.

    function CountProvider(props) {
      const [count, setCount] = React.useState(0)
      const value = React.useMemo(() => {
        return {
          count,
          setCount,
        }
      }, [count])
      return <CountContext.Provider value={value} {...props} />
    }

    function component:

    Check the props, only do the re-render when props changes.

    const TodoItem = React.memo(({ todo, onChange, onDelete }) => {
      console.log("TodoItem5", { todo, onChange, onDelete });
      const theme = useContext(ThemeContext);
      return (
        <Item key={todo.id} theme={theme}>
          <Checkbox
            id={todo.id}
            label={todo.text}
            checked={todo.completed}
            onChange={onChange.bind(this, todo.id)}
          />
          <Button onClick={onDelete.bind(this, todo.id)} theme={theme}>
            x
          </Button>
        </Item>
      );
    });
  • 相关阅读:
    uboot misc
    Android Q dynamic partition
    Android Verified Boot 2.0简要
    dts参考资料
    SWT问题分析总结
    lzma文件及命令简介
    C++之继承
    C++之常指针
    C++之对象成员指针
    C++之对象指针
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10829020.html
Copyright © 2011-2022 走看看