I'm learning redux and I'm trying to get it to work with a react-bootstrap modal. I want the modal to show when the state in redux is set to true. However, when I try to get the state from redux the modal never shows even though the state changes to true.
It works fine when using regular state components (useState, setState).
Is there a reason the following code doesn't work?
Example React Component:
const Example = () => {
const showStore = createStore(showStore.reducer)
const handleShow = () => {
showStore.dispatch({type: 'SHOW'})
}
const handleClose = () => {
showStore.dispatch({type: 'HIDE'})
}
return (
<>
<Button onClick={handleShow}>
Show modal
</Button>
<Modal show={showStore.getState()} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>Modal title</Modal.Title>
</Modal.Header>
<Modal.Body>
<Form>
<Form.Label>Insert text</Form.Label>
<Form.Control type="text" placeholder="Sample text" />
<Modal.Footer>
<Button onClick={handleClose}>
Cancel
</Button>
</Modal.Footer>
</Form>
</Modal.Body>
</Modal>
</>
)
}
export default Example
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
showStore:
const reducer = (state = false, action) => {
switch (action.type) {
case 'SHOW' :
return true
case 'HIDE' :
return false
default:
return state
}
}
export default { reducer }