React 使用render props实现跨层传参时组件未随props更新
React组合模式下的跨层组件交互实现
假设正在使用组合而非继承的开发模式,突然需要让中间层级的组件和它所包含的子组件产生交互。
如果子组件在渲染前需要与父组件通信,你可以进一步使用render props来实现。
上述内容引自React官方Context文档的使用前置说明章节。
核心需求:我需要让Top和Intermediate组件分别控制Bottom组件的不同props。
已知组件的HTML嵌套层级为:Top组件包裹Intermediate组件,Intermediate组件再包裹Bottom组件。
复现说明
可检索对应StackBlitz项目名(react-props-not-rendering-issue)打开在线调试,对应的问题实现代码如下:
import React, {useState} from 'react'; export function Top() { const [a, sA] = useState(false); function renderBottom(b: boolean) { return ( <Bottom a={a} b={b}/> ); } return ( <div> <div>A in TOP: {a ? 'true' : 'false'}</div> <button onClick={e => sA(!a)}>A</button> <Intermediate renderBottom={renderBottom}/> </div> ); } export function Intermediate(p: {renderBottom: any}) { const [b, sB] = useState(false); const BottomComponentWithASet = p.renderBottom; return ( <div style={{background: 'red'}}> <div>B in Intermediate: {b ? 'true' : 'false'}</div> <button onClick={e => sB(!b)}>B</button> <BottomComponentWithASet b={b}/> </div> ); } export function Bottom(p: {a: boolean; b: boolean; }) { console.log(p.b); // 日志输出的b值符合预期,初始为false,点击按钮后变为true return ( <div> <div>A: {p.a ? 'true' : 'false'}</div> {/* 但下方展示的B值始终为true,不会随状态更新 */} <div>B: {p.b ? 'true' : 'false'}</div> </div> ); }
之前尝试过把a和b对应的状态全部放到Top组件里统一处理的方案,这种写法虽然可以正常运行,但会把大量仅和Intermediate组件相关的逻辑全部堆积到Top组件中(此处为简化演示代码,实际业务场景下代码冗余的问题会更突出)。对应的旧实现代码如下:
import React, {useState} from 'react'; export function Top() { const [a, sA] = useState(false); // 这部分状态本只和Intermediate、Bottom组件相关,却被放到了顶层组件中 const [b, sB] = useState(false); return ( <div> <div>A in TOP: {a ? 'true' : 'false'}</div> <button onClick={e => sA(!a)}>A</button> <Intermediate> <div>B in Intermediate: {b ? 'true' : 'false'}</div> <button onClick={e => sB(!b)}>B</button> <Bottom a={a} b={b}/> </Intermediate> </div> ); } export function Intermediate(p: any) { return ( <div style={{background: 'red'}}> { p.children } </div> ); } export function Bottom(p: {a: boolean; b: boolean; }) { console.log(p.b); return ( <div> <div>A: {p.a ? 'true' : 'false'}</div> <div>B: {p.b ? 'true' : 'false'}</div> </div> ); }
问题根因
当前代码对render props的使用存在语法理解偏差:
- Render props本质是返回React节点的函数,不能像普通自定义组件一样用JSX标签语法传参。
- 原代码中把
renderBottom赋值给BottomComponentWithASet后,用<BottomComponentWithASet b={b}/>的方式渲染,等价于让React把整个props对象传入renderBottom函数。此时renderBottom的形参b实际收到的是{b: 布尔值}类型的对象,无论真实状态是true还是false,这个对象永远是真值,最终传入Bottom组件的b值就会始终为true,无法正确跟随状态变化更新。
修正方案
不需要调整状态分层结构,只需要把render prop的渲染方式从JSX标签语法改成直接函数调用,把b值作为参数传入即可。优化后的Intermediate组件代码如下:
// 补充明确的TS类型标注,避免类似的参数传递错误 export function Intermediate(p: {renderBottom: (b: boolean) => React.ReactNode}) { const [b, sB] = useState(false); return ( <div style={{background: 'red'}}> <div>B in Intermediate: {b ? 'true' : 'false'}</div> <button onClick={e => sB(!b)}>B</button> {/* 直接调用render prop,传入当前b状态值 */} {p.renderBottom(b)} </div> ); }
这种写法完全保留了状态分层的优势:
a状态完全由Top组件管理,不需要下放到中间层b状态完全由Intermediate组件管理,不需要上提到顶层Bottom组件可以同时接收两个层级传入的props值,实时响应状态变化
内容的提问来源于stack exchange,提问作者Ambroise Rabier
相关产品推荐
相关产品推荐

