Я использую 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 };
};
}