I'm fetching data from an API call. So on componentWillMount
I'm making the API call which is mappedDispatchToProps
: fetchCats
class Cats extends Component {
constructor(props){
super();
this.state = {
toPass: 0
}
}
componentWillMount() {
this.props.fetchCats(this.state.toPass);
};
getCats(){ // <= Is this the proper way to update the state and call the api?
this.setState({
toPass: this.state.toPass+2 //<= increment by 2
});
this.props.fetchCats(this.state.toPass);
}
renData() {
//map the array this.props.categories and return data
}
render(){
const { categories } = this.props.categories
console.log("CATEGORIES: ", categories);
return (
<View>
<View>
<Button
name="PressMe"
onPress={() => this.getCats()}>
</Button>
</View>
<View>
{this.reData()}
</View>
</View>
)
}
}
So the first time when the page loads toPass = 0
. When I click the button, the value passed is 2 but I'm getting the same results for categories
. The results are supposed to append to the categories array in the Redux
state. But after the first press everything works fine, except have double entries of the results for toPass=0
. What is wrong with my getCats()
function?
Many thanks.