亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

怎么傳遞props

怎么傳遞props

翻閱古今 2019-02-02 15:35:42
我在父組件發送了請求,返回了一些數據,但是現在子組件收不到數據,請問通過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    })
查看完整描述

1 回答

?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

父組件獲得了數據后 將你的數據存儲在父組件的state當中 ,然后向子組件傳遞通過state去傳遞就好了

如:



//父組件 Parent

class Parent extends React.Component{

    constructor(props){

        super(props);

        this.state={

            parentDate:""

        }

    }

    handle(){

        //在這里請求拿到數據然后進行存儲

        this.setState({

            parentDate:"XXXX"

        })

    }

    render(){

        return (

            <Child  parentDate={this.state.parentDate}/>

        )

    }

    //子組件

    class Child extends React.Component{

        constructor(props){

            super(props);

        }

        componentWillReceiveProps(nextProps){

            //在這里可以將接受到的props去存在子組件內

        }

        render(){

            return (

                <div>{"I get : "+this.props.parentData}</div>

            )

        }

    }

}


查看完整回答
反對 回復 2019-02-13
  • 1 回答
  • 0 關注
  • 386 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號