I have a reducer that gives me carReducerwhich I can use as props in the EditCar component:
My carReducer:
export default function carReducer(state = {}, action) {
switch (action.type) {
case 'GET_SINGLECAR':
return {
...state,
next: action.payload
}
break;
}
return state;
}
And in the EditCar component, I declared carReduceras:
function mapStateToProps(state) {
return {
carReducer: state.carReducer
}
}
export default connect(mapStateToProps, mapDispatchToProps)(EditCar);
Now in the same component I want to update my intialState cardatawith this carReducerbefore the component loads.
constructor(props) {
super(props);
this.state = {
cardata: {}
}
}
I tried to use componentWillReceiveProps, but it gives me undefinedboth in nextProps.carReducerand this.props.carReducer.
componentWillReceiveProps(nextProps) {
if (nextProps.carReducer != this.props.carReducer) {
this.setState({ cardata: nextProps.carReducer });
}
}
I am new to react-redux, so any help is greatly appreciated. Thank.
source
share