2017-02-16 3 views
1

Я использую ReactNative с Redux, и у меня есть редуктор, который возвращает массив значений. У меня есть следующий компонент, который должен получить этот массив, заданный редуктором.prop undefined внутри componentWillReceiveProps() method

Я могу успешно распечатать весь массив внутри mapStateToProps и внутри метода render() Я могу видеть часть этого массива. Проблема заключается в том, что внутри 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}) 
} 

В исходном коде вы используете существующие опоры, следовательно, вы не видите обновление.