I'm using React Native 0.43. I've two components, named ParentComponent
and ChildComponent
. I want to pass some props from parent to child component. I'm using following code (abridged version) in parent component:
export default class ParentComponent extends Component {
constructor(props) {
super(props);
this.state = {
latitude: 34.7821,
};
}
render() {
return (
<View>
<ChildComponent latitude={this.state.latitude} />
</View>
);
}
}
My child component is as following:
export default class ChildComponent extends Component {
constructor(props) {
super(props);
this.state = {
latitude: props.latitude,
};
}
componentWillMount() {
console.log('Before Mount: ' + this.state.latitude)
}
render() {
return (
<Text>{'Mounted: ' + console.log(this.state.latitude)}</Text>
);
}
}
Now, my console shows following result:
2:14:12 AM: Before Mount: null
2:14:12 AM: Mounted: null
2:14:12 AM: Mounted: 34.7821
Now componentWillMount()
in my original code has an API call to a web service which depends on the value of this.state.latitude
which clearly is not been passed, at least on the first render. On second render, when this.state.latitude
value become available, only the render()
function executes, but i need this value in my componentWillMount()
function.
What I'm doing wrong here?