class IssueRecordUpload extends React.Component {
constructor(props) {
super(props)
this.state = {
filters: {},
}
}
static propTypes = {
filters: PropTypes.object,
dispatch: PropTypes.func
}
componentDidMount() {
const {filters} = this.props
this.setState({
filters: filters
}, () => {
this.load()
})
}
componentWillReceiveProps(next) {
const {filters} = this.state // filters = undefined
if (filters.toString() !== next.filters.toString()) {
this.setState({
filters: next.filters,
}, () => {
this.load()
})
}
}
load = () => {
const {dispatch} = this.props
const {filters} = this.state
dispatch(filters)
}
}
filters and dispatch are passed in from the parent component. When DidMount, setState () props.filter-> state.filters, receives a new props, in WillReceiveProps and finds that the original state.filters is undefined, and setState fails, but next.filters exists. Solve the reason. Thank you, boss!