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>
      );
    });
  • 相关阅读:
    springboot maven打包插件
    maven打包指定main入口插件
    团队开发环境一致性性要求
    springboot 在idea中实现热部署
    IDEA 2018.1可用License服务(持续更新)
    IDEA打jar包
    3月18号
    3月17号
    3月16号
    3月13号
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10829020.html
Copyright © 2011-2022 走看看