Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React-this.props.children #26

Open
PleaseStartYourPerformance opened this issue May 18, 2021 · 0 comments
Open

React-this.props.children #26

PleaseStartYourPerformance opened this issue May 18, 2021 · 0 comments

Comments

@PleaseStartYourPerformance
Copy link
Owner

PleaseStartYourPerformance commented May 18, 2021

有点类似Vue中的slot插槽

props是我们从上个组件传值传下来的集合,react是单向数据流,每一个props都应该对应的是从上个组件传过来的值。但是这里有一个例外,那就是this.props.children

this.props.children 的值有三种可能:

  1. 如果当前组件没有子节点,它就是 undefined ;
  2. 如果有一个子节点,数据类型是 Object;
  3. 如果有多个子节点,数据类型就是 Array。

React 提供一个工具方法 React.Children 来处理 this.props.children 。可以用 React.Children.map 来遍历子节点,而不用担心 this.props.children 的数据类型是 undefined 还是 Object Array

image

this.props.children还涉及到 回调渲染模式 后面章节会提到

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant