ReactJs中的this.props.children总结

本文介绍了在React中如何利用this.props.children属性处理组件的子元素,并演示了使用React.Children.map遍历子节点的方法。

this.props 对象的属性与组件的属性一一对应,但是有一个例外,就是 this.props.children 属性。它表示组件的所有子节点

var NotesList = React.createClass({
  render: function() {
    return (
      <ol>
      {
        React.Children.map(this.props.children, function (child) {
          return <li>{child}</li>;
        })
      }
      </ol>
    );
  }
});

ReactDOM.render(
  <NotesList>
    <span>hello</span>
    <span>world</span>
  </NotesList>,
  document.body
);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

上面代码的 NoteList 组件有两个 span 子节点,它们都可以通过 this.props.children 读取,运行结果如下。

  1. hello
  2. world

这里需要注意, this.props.children 的值有三种可能:如果当前组件没有子节点,它就是 undefined ;如果有一个子节点,数据类型是 object ;如果有多个子节点,数据类型就是 array 。所以,处理 this.props.children 的时候要小心。 
React 提供一个工具方法 React.Children 来处理 this.props.children 。我们可以用 React.Children.map 来遍历子节点,而不用担心 this.props.children 的数据类型是 undefined 还是 object。更多的 React.Children 的方法,请参考官方文档 https://facebook.github.io/react/docs/top-level-api.html#react.children 。

var HelloWorld = React.createClass({
     render:function(){
        return (
             <ul>
              {
                   React.Children.map(this.props.children, function (value,key) {
                          return <li>{value}----{key}</li>;
                   })
               }
             </ul>
        );
     }
 });
 ReactDOM.render(
   <HelloWorld>
     <span>思考思考</span>
     <span>瞬间即逝</span>
     <span>阿达瓦得</span>
   </HelloWorld>,
   document.getElementById('root'));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

输出结果为: 
思考思考—-0 
瞬间即逝—-1 
阿达瓦得—-2

map遍历数组

var names = ['Alice', 'Emily', 'Kate'];
ReactDOM.render(
  <div>
  {
    names.map(function (name) {
      return <div>Hello, {name}!</div>
    })
  }
  </div>,
  document.getElementById('example')
);
React 的类组件中,`this.props` 是一个对象,它包含了父组件传递给当前组件的所有属性。通过 `this.props`,组件可以访问这些属性并在其渲染逻辑中使用它们。 `this.props` 是单向数据流的一部分,意味着数据只能从父组件流向子组件,子组件不能直接修改 `this.props` 中的值。这有助于保持数据流动的可预测性组件之间的解耦。 例如,有一个父组件一个子组件: ```jsx // 子组件 class Text extends React.Component { constructor() { super(); } render() { const { name, age } = this.props; // 解构 return ( <div> <h2>我的名字:{name}</h2> <h2>我的年龄:{age}</h2> </div> ); } } // 父组件 class App extends React.Component { constructor(props) { super(props); this.state = { name: 'xjj', age: '18' }; } render() { return ( <Text name={this.state.name} age={this.state.age} /> ); } } ``` 在这个例子中,父组件 `App` 通过 `name` `age` 属性将数据传递给子组件 `Text`,子组件 `Text` 可以通过 `this.props.name` `this.props.age` 来访问这些数据[^2]。 还有一个特殊的属性 `this.props.children`,它表示组件标签之间的子节点。例如: ```jsx class Test extends React.Component { render() { return ( <div> <h1>Hello World</h1> {this.props.children} </div> ); } } // 使用 Test 组件 <Test>a</Test> <Test /> <Test> <div>1</div> <div>2</div> </Test> ``` 在这个例子中,`this.props.children` 根据不同的使用情况有不同的值:当 `<Test>a</Test>` 时,`this.props.children` 是字符串 `a`;当 `<Test />` 时,`this.props.children` 是 `undefined`;当 `<Test><div>1</div><div>2</div></Test>` 时,`this.props.children` 是一个包含两个 `div` 元素的数组[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值