I am using React and Redux in a project I’ve two components a parent and a child.
The parent component passes some props to the child when the child component receives those props it calls some action which change some of the props (async) that the parent passed to its child. Now my redux-store shows that the data has successfully stored in it. But my child component doesn’t re-render itself.
Parent Component:
class Parent extends React.Component {
getChilds(){
let child = [];
let i = 0;
for (let keys in this.props.home.data) {
child[i] = (
<Child title={keys} data={this.props.home.data[keys]} key={keys} />
);
i++;
if (i === 6) break;
}
return Rows;
}
render(){
return (
<div>
<h1>I am gonna call my child </h1>
{this.getChilds()}
</div>)
}
}
Child Component:
class Child extends React.Component {
// Here I'm not sure which lifecycle method to use to i'm gonna use
// componentDidMount
componentDidMount(){
if(this.props.data.items.length === 0){
// calling an action to fill this.props.data.items array with data
this.props.getData(this.props.data.id);
}
}
getGrandSong(){
let grandSons = [];
if(this.props.data.items.length > 0){
grandSons = this.props.data.items.map( item => <GrandSon item={item}
/>);
}
return grandSons;
}
render(){
return (
<div>
<h1> I am the child component and I will call my own child </h1>
{this.getGrandSon()}
</div>
)
}
Props are updated when I check the props in react-dev tool.
this.props.getDatain the Child, but you don't pass such propgetDatain the Parent when you call<Child .. />. Second, I don't see any redux on the scene. Third, there are typos like defininggetGrandSongthen callinggetGrandSon(). I think this piece of code wouldn't work for many such reasons. - amikmapStateToProps/mapDispatchToProps) - Dan D