在effect中set数据 会导致组件更新了两次 怎么处理?-灵析社区

577739990

我在props中有一个数据data 一秒变一次 当前组件有一个isActive 在isActive为true时 保存它以作为缓存 用useEffect监听props.data再set到useState中 组件会更新两次。。。白费性能 子组件越多就越卡

阅读量:243

点赞量:20

问AI
或许可以借助"useRef",把组件改为完全受控的形式: function Test3({ data, isActive }){ const storedData = useRef(data); storedData.current = useMemo( () =>( isActive? data: storedData.current), [isActive, data] ); return {storedData.current} }