React 父组件给子组件传值,子组件接收

父组件传值代码:

  render() {
    return (
      <div>
        {this.state.list?(<GeomLine list={this.state.list}/>):null}
      </div>
    );
  }

子组件接收代码:

class GeomLine extends Component {

  // 在组件接收到一个新的 prop (更新后)时被调用。这个方法在初始化render时不会被调用。
  componentWillReceiveProps(props){
    console.log('props',props.list)
    this.setState({
      name:props.list[0].name
    })
  }
  render() {
    return(
     {this.state.name ? (<div>{this.state.name}</div>) : null}
    )
  }
}
export default GeomLine;

猜你喜欢

转载自blog.csdn.net/qq_35713752/article/details/103278336