且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何仅渲染子子项而不渲染所有高级组件

更新时间:2023-12-02 19:54:34

You should use memoization (useCallback/React.memo) and rewrite handle logic with functional updates.

Also, you avoid Child to render, since you have a new value after rendering.

// Make a stable callback
const onChangeHandle = useCallback((event, id) => {
  setSelectedChild((updatedArray) => {
    if (event.target.checked) {
      if (!updatedArray.includes(id)) {
        return [...updatedArray, id];
      }
    } else {
      return updatedArray.filter((currId) => currId !== id);
    }
    return updatedArray;
  });
}, []);

// Memoize the component
const MemoChild = React.memo(Child);
const MemoSubChild = React.memo(SubChild);

登录 关闭
扫码关注1秒登录
如何仅渲染子子项而不渲染所有高级组件
发送“验证码”获取 | 15天全站免登陆