2015-12-07 55 views
1

我需要對子組件的嵌套對象數組進行更改。該數組作爲道具從父組件傳遞下來,我想在列表中找到一個項目並對其進行更改,以便當我回到父場景時,更改將被保存。我有以下代碼,但出現此錯誤:undefined is not an object (evaluating \'this.state')。任何人都可以帶領我正確地解決這個問題嗎?更改從React Native中的父組件傳遞的道具數組

submitChange() { 
    //update the cart list 
    //hide the panel 
    this.props.cartList.forEach(function(arrayItem) { 
    if(arrayItem.prod.product === this.state.name) { 
     arrayItem.quantityOrdered = this.state.quantity; 
    } 
    console.log(arrayItem.prod.product + " " + arrayItem.quantityOrdered) 
    }) 
    console.log(" this item" + this.state.quantity + ", " + this.state.name) 
    //this.props.hidePanel() 
} 

回答

1

我不知道陣營本地的,但在作出反應,你將有

父組件

getInitialState: function(){ 
    var carList = [.....]; 
}, 

changeCarItem: function(item){ 
    //first get the original 
    //find the item you want to modify 
    //after the item is found, make the change to your originalItem(carlist) 
    //you need to use **this.setState()**, in-order to affect the main data 
} 

render: function(){ 
    <Child list={this.state.carList} changeCarItem={this.changeCarItem}> 
    ...... 
} 

輔元件

//perform your logic 
//use this.props.changeCarItem(item) 

所以要總結,您需要傳遞父代的回調,在此例中爲 「changeCarItem」,並使子組件調用此回調。

http://facebook.github.io/react/tips/communicate-between-components.html),看看這個

相關問題