我有一个Parent
组件和Child
一个组件,并且我有触发子组件中的一些 api 调用的操作,并且componentWillMount
我检查了if
一些props
来自父组件并执行一些触发器的条件。如果条件为真,我会触发一个渲染新组件的方法。问题是在子组件中componentWillmount
并props
this.props.person
没有this.props.notFound
定义,但我需要在渲染之前等待 api 请求并检查这个道具。
家长:
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,
)}
孩子们:
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)