2017-02-16 2 views
1

私はReduxでReactNativeを使用していますが、値の配列を返す減速器があります。 私は以下のコンポーネントを用意しています。componentWillReceiveProps()メソッド内でpropが未定義です

mapStateToPropsrender()の中に配列全体を正常に印刷できます。この配列の一部を見ることができます。 問題は、内部でcomponentWillReceiveProps()の配列が定義されていないことです。

class MyComponent extends Component { 
     constructor(props) { 
     super(props); 
     } 

     componentWillReceiveProps(){ 
     console.log("hey"); // I can see this in the terminal 
     const { calculatedPercentages } = this.props; 
     // here's the problem I get undefined 10 times 
     for (let i=1; i<11; i++){ 
      console.log("test: "+calculatedPercentages[i]); 
     } 
     this.setState({myPercentages:calculatedPercentages}) 
     } 

     render() {  
     return (
      <View> 
      <Text>{this.props.calculatedPercentages[3]}</Text> // this works and it prints out the correct value 
      </View> 
     ); 
     } 


    const mapStateToProps = ({ myRed }) => { 
     const { calculatedPercentages } = myRed; 
     // this loop works correctly, it shows me the 10 values I've inside the array 
     for (let i=1; i<11; i++){ 
      console.log(calculatedPercentages[i]); 
     } 
     return { calculatedPercentages }; 
    }; 
} 

答えて

0

componentWillReceivePropsメソッドが最初のパラメータとして小道具の次のセットを渡され、あなたはとてもようにそれを利用する必要があります。したがって、既存の小道具を使用している元のコードで

componentWillReceiveProps (nextProps) { 
    const { calculatedPercentages } = nextProps; 

    for (let i=1; i<11; i++){ 
    console.log("test: "+calculatedPercentages[i]); 
    } 

    this.setState({myPercentages:calculatedPercentages}) 
} 

更新が表示されません。

関連する問題