i am using react and redux for my application. My problem is i receive 2 time my props the first is undefined and the second is ok. So i cannot use the props.
import ...
class Home extends Component {
//some code logic
render(){
console.log(this.props.header);
const header = this.props.header;
return (
<Welcome title={header.title} />
)
}
}
const mapStateToProps = (state) => {
return {
header: _.find(state.header, { 'page': 'accueil' })
}
}
export default connect(mapStateToProps)(Home);
console.log give me 2 answer : 1 / undefined 2 / Array of obj (that what i need)
Uncaught TypeError: Cannot read property title of undefined

state.headerbeing set? After an API call? It might be as simple as addingif (!this.props.header) return nullto the start of therenderfunction, but that might only hide the issue and not fix the cause. - Michael Peyperundefinedthe first time it renders (it hasn't got the data yet). returningnullisn't doing a "loop" inside the component, it is saying there is noting to render, which there isn't in the case of the initial render. You could also return some kind ofLoadingcomponent instead ofnullfor a better user experience. - Michael Peyper