[Suggestion]: Documentation does not make mention of the difference between `props.children` and directly rendering children as it relates to rendering behaviour.
还没有人认领这个 Issue。
- 主要语言
- JavaScript
- 星标
- 11.8k
- 派生
- 7.9k
- 平均合并
- 1 天 11 小时
- 30 天内合并 PR
- 11
描述
Summary
Here I have an example of two ways we can create functionally identical components.
export function ChildrenStyleOne() {
const [value, setValue] = useState('');
return <div id="foo">
<SomeThing />
</div>
}
export function ChildrenStyleTwo(props: React.PropsWithChildren) {
const [value, setValue] = useState('');
return <div id="bar">
{props.children}
</div>
}
However, there is an important different in how these components behave as far as rendering behaviour goes.
In the first style, every render of ChildrenStyleOne (eg. say because state is changing), will also cause a render of SomeThing.
In the second style, additional renders of ChildrenStyleTwo will not cause renders of the {props.children}.
This is a useful distinction to be aware of - using the props.children/slots approach is a simple mechanism to avoid full branch renders.
I've observed people commonly believing that 'every time a context provider's state changes, everything below it will render' - and I believe this misconception arrises from not being aware of this difference in rendering behaviour.
Page
https://react.dev/learn/understanding-your-ui-as-a-tree
Details
The docs above actually serve to reinforce the conflation of directly rendered children,and props.children.
For example the example code includes this node:
<InspirationGenerator>
<Copyright year={2004} />
</InspirationGenerator>
and the render tree diagram looks like this:
We can see that FancyText here is a directly rendered child, while Copyright is a props.children child.
The render tree described in the docs makes no distinction between the two.
I imagine this is likely a deliberate decision on the the part of the React maintainers - simplifying the conceptual model. If that's the case, is there a blog post or something that talks about what the strategy is?
The docs perhaps could do with a deep dive note that makes mention of this distinction or links to a more comprehensive optimisation section elsewhere.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从“Understanding your UI as a tree”页面开始,检查 InspirationGenerator 示例,尤其是直接渲染的 FancyText 和基于 props.children 的 Copyright 子项。确定文档是否应解释它们在渲染上的区别,或链接到更深入的优化资源。当页面准确处理这一差异,或清楚解释预期的简化模型时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, react
- 领域
- documentation
- Issue 类型
- 文档
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100