I know there are a lot of same questions posted in this site but none of them links to my issue. This is close to mine but I am not working with DB yet. Just a complete react/redux
implementation.
I am implementing redux in my sample project for reactstrap
modal
and confuses me because one of my reducers not updating the state the first time. Consider my redux setup.
Action
export const showModal = (data) => {
return {
type: MODAL_TOOL,
payload: data
};
}
Reducer
const initialState = {
isOpen: false
};
export default function(state=initialState, action) {
switch(action.type) {
case MODAL_TOOL:
console.log(action.payload.isOpen); //logs true
return {
...state,
isOpen: action.payload.isOpen
}
default:
return state;
}
}
Combine Reducer
export default combineReducers({
modal: modalReducer
});
Component
class TaskModel extends Component {
state = {
isOpen: this.props.modal.isOpen
}
onCreateTask = () => {
this.props.showModal({
isOpen: !this.props.modal.isOpen
});
this.setState({
isOpen: this.props.modal.isOpen
});
}
render() {
return(
<Button
size="sm"
color="dark"
style={{marginBottom: '2rem'}}
onClick={this.onCreateTask}
>
Add Task
</Button>
)
}
}
const mapStateToProps = (state) => ({
modal: state.modal
});
export default connect(mapStateToProps, { showModal })(TaskModel);
When I click the button Add Task
the first time, it will not update the state even the log from reducer showing true. It works the 2nd time and update the redux
state
. My CRUD for the tasks is working fine with the same redux
setup. Did I misunderstand something?