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