@home-app/redux-modal.module
v1.0.0
Published
Modal
Downloads
19
Readme
Homeless Modal
Universal Modal that uses redux state
How to use
- Install module:
yarn add @home-app/redux-modal.module
- Add redux state:
import { reducer as modalReducer } from '@home-app/redux-modal.module';
rootReducer = combineReducers({
potato: potatoReducer,
tomato: tomatoReducer,
...
modal: modalReducer,
})
- Create modal config
export const modalIds = {
EXAMPLE_MODAL: 'EXAMPLE_MODAL',
};
const modalConfig = {
[modalIds.EXAMPLE_MODAL]: props => <ExampleModal {...props} />,
};
- Create ModalComponent and put it somewhere inside react root element
import { modalFactory } from '@home-app/redux-modal.module';
const Modal = modalFactory({
reduxName: 'modal', // name of reducer from step 1 (default modal)
config: modalConfig, // modal config from step 2
appElement: '#root', // selector of react root element
classNamePrefix: 'redux-modal', // css classNames prefix (default redux-modal)
});
export default RootComponent = props => <div>
<OtherStuff />
<Modal />
</div>
- Connect action creators to component from step 3
import { actions } from '@home-app/redux-modal.module';
...
const mapDispatchToProps = dispatch => ({
openModal: (id, params) => dispatch(actions.modalOpen(id, params),
});
export default connect(null, mapDispatchToProps)(RootComponent));
- Add openHandlers wherever you want
...
<input
type="button"
onClick={() => this.props.openModal(modalIds.EXAMPLE_MODAL)}
value="PUSH ME"
/>
second argument for openModal() will be passed to modal component as props