猿问

怎么传递props

我在父组件发送了请求,返回了一些数据,但是现在子组件收不到数据,请问通过props怎么传递,具体的代码实现或是简单的案例


...... {


constructor(props) {

    // console.log(props); **这里输出没有得到数据thumbnails:{total: 0, url: Array(0)}**

    super(props);

    this.state = {

        thumbnail:[]

    };

};

render() {

return (

..........


componentWillReceiveProps(nextProps){

    //debugger

    console.log(nextProps);    //没有数据

    this.setState({

        thumbnail:nextProps.thumbnail

    })


翻阅古今
浏览 368回答 1
1回答

素胚勾勒不出你

父组件获得了数据后 将你的数据存储在父组件的state当中 ,然后向子组件传递通过state去传递就好了如://父组件 Parentclass Parent extends React.Component{&nbsp; &nbsp; constructor(props){&nbsp; &nbsp; &nbsp; &nbsp; super(props);&nbsp; &nbsp; &nbsp; &nbsp; this.state={&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parentDate:""&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; handle(){&nbsp; &nbsp; &nbsp; &nbsp; //在这里请求拿到数据然后进行存储&nbsp; &nbsp; &nbsp; &nbsp; this.setState({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parentDate:"XXXX"&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }&nbsp; &nbsp; render(){&nbsp; &nbsp; &nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Child&nbsp; parentDate={this.state.parentDate}/>&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; }&nbsp; &nbsp; //子组件&nbsp; &nbsp; class Child extends React.Component{&nbsp; &nbsp; &nbsp; &nbsp; constructor(props){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(props);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; componentWillReceiveProps(nextProps){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //在这里可以将接受到的props去存在子组件内&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; render(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>{"I get : "+this.props.parentData}</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答