I have a Parent
component and Child
one, and I have action that trigger some api call in Child component and on componentWillMount
I check in if
condition some props
that come from parent and do some trigger. If condition true I trigger a method that render new component. The problem is that in child component in componentWillmount
the props
this.props.person
and this.props.notFound
come undefined, but I need wait for api request before render and check this props.
parent:
export class Parent extends Component {
state = {
id: this.props.id || ''
}
render() {
<Child value={this.state.value} onNotValidId={() => this.renderNewComponent()} />
}
}
export const mapStateToProps = state => ({
tagStatus: state.tagAssignment.status,
persons: state.entities.persons,
)}
Children:
export class Child extends Component {
componentWillMount = () => {
this.props.init(parseInt(this.props.id, 16), this.props.accessToken)
if (!this.props.notFound && !this.props.person)
this.props.onNotValidId()
}
render = () => {
return (
<div>Some body</div>
)
}
}
export const mapStateToProps = state => ({
status: state.tagAssignment.status,
person: state.entities.persons[state.tagAssignment.assignee],
accessToken: state.auth.accessToken,
})
export const mapDispatchToProps = dispatch => ({
init: (id, accessToken) => dispatch(checkId(id, accessToken)),
})
export default compose(
connect(mapStateToProps, mapDispatchToProps),
)(Child)