React.memo包裹的Child组件props未变仍重渲染且比较函数未触发
问题根因
- 核心问题为
WindowForm组件被定义在Parent组件的函数作用域内部 - 每次
Parent因状态更新(如勾选通知框触发isNotifyMe状态变更)重新渲染时,都会在内存中生成一个全新的WindowForm函数引用 - React做DOM diff时,会对比前后两次渲染的组件类型引用:两次的
WindowForm引用不一致时,会直接判定这是完全独立的组件节点,执行旧组件树(包括被React.memo包裹的Child组件)的卸载流程,再挂载全新的组件树 - 全量重挂载的流程完全不会触发
React.memo的props对比逻辑,因此写在areEqual中的日志永远不会打印,Child组件也会随整个树的重挂载被重新渲染
修复方案
- 优先方案:将
WindowForm的定义移到Parent组件外部,保持引用稳定
由于WindowForm依赖Parent内部的状态与回调,可通过props将所需依赖传入外部定义的组件,示例代码:// 平级定义WindowForm,引用全局稳定 function WindowForm({ selected, isRunNow, isNotifyMe, handleSchedule, handleNotify }) { return ( <div> {/* 原WindowForm内部的JSX逻辑完整保留 */} <Row> <Col span={12}> <label className={styles.labelWeight}> Name : </label> <input type="text" value={selected.name} readOnly={true} className={styles.input} /> <br></br> <br></br> <label className={styles.labelWeight}>Description : </label> <input type="text" value={selected.description} readOnly={true} className={styles.input} ></input> <br></br> <br></br> <input type="checkbox" name="runImm" id="runImm" checked={isRunNow} onChange={handleSchedule} ></input> <label> Schedule as soon as possible</label> </Col> <Col span={12}> <input type="checkbox" name="notifyProcess" id="notifyProcess" checked={isNotifyMe} onChange={handleNotify} ></input> <label> Notify me when this process ends</label> <br></br> <br></br> <label>Submission Notes : </label> <textarea name="notes" id="notes" rows="4" cols="50" disabled={!isNotifyMe} ></textarea> </Col> </Row> <Row> <Col span={24}> <Child isRunNow={isRunNow} /> </Col> </Row> </div> ) } function Parent({ selected, show, setShow }) { const [isRunNow, setIsRunNow] = useState(true); const [isNotifyMe, setIsNotifyMe] = useState(false); const handleNotify = useCallback(() => { setIsNotifyMe(!isNotifyMe); }, [isNotifyMe]); const handleSchedule = useCallback(() => { setIsRunNow(!isRunNow); }, [isRunNow]); return ( <Modal visible={show} width={800} centered maskClosable={false} onCancel={() => setShow(false)} title="JKM" > <WindowForm selected={selected} isRunNow={isRunNow} isNotifyMe={isNotifyMe} handleSchedule={handleSchedule} handleNotify={handleNotify} /> </Modal> ); } - 临时方案:如果受业务限制必须在
Parent内部组织表单逻辑,可使用useMemo缓存表单的JSX内容,避免每次重渲染生成新的组件节点
示例代码:function Parent({ selected, show, setShow }) { const [isRunNow, setIsRunNow] = useState(true); const [isNotifyMe, setIsNotifyMe] = useState(false); const handleNotify = useCallback(() => { setIsNotifyMe(!isNotifyMe); }, [isNotifyMe]); const handleSchedule = useCallback(() => { setIsRunNow(!isRunNow); }, [isRunNow]); // 缓存表单JSX,仅当依赖变化时才重新生成 const windowFormContent = useMemo(() => { return ( <div> {/* 原WindowForm内部的JSX逻辑完整移至此处 */} <Row> <Col span={24}> <Child isRunNow={isRunNow} /> </Col> </Row> </div> ) }, [selected, isRunNow, isNotifyMe, handleSchedule, handleNotify]) return ( <Modal visible={show} width={800} centered maskClosable={false} onCancel={() => setShow(false)} title="JKM" > {windowFormContent} </Modal> ); }
补充说明:修复后Child组件的
React.memo对比逻辑会正常触发,当isRunNow值未变化时,即使Parent其他状态更新,也不会触发Child的重渲染。
内容的提问来源于stack exchange,提问作者BNK
相关产品推荐
相关产品推荐

