Answer the question
In order to leave comments, you need to log in
How to make a modal window in react & redux?
I want to make a modal window on button click, with a form for adding a new object. How to achieve this based on react and redux?
Answer the question
In order to leave comments, you need to log in
It is not possible to attach redux to this case.
Here is the container code:
let name;
let description;
const customStyles = {
content : {
...
}
};
let LeftMenu = ({modalIsOpen, firstButtonClick, close}) => {
return (
<div>
<nav >
<ul className='LMenu'>
<MenuButton text = {FIRST_BUTTON} onClick = {() => firstButtonClick(<Modal/>)}/>
...
</ul>
</nav>
<Modal
isOpen={modalIsOpen}
onRequestClose={close}
style={customStyles}
>
<h2 ref='subtitle'>Hello</h2>
<button onClick={close}>close</button>
<div>I am a modal</div>
<form onSubmit={e => {
e.preventDefault();
createFamily(name.value, description.value)
}}>
<ul>
<li><input placeholder='Введите название семьи'
ref={node1 => {name = node1}}/></li>
<li><input placeholder='Введите описание семьи'
ref={node2 => {description = node2}}/></li>
<button type='submit'>Создать</button>
</ul>
</form>
</Modal>
</div>
);
};
LeftMenu.propTypes = {
modalIsOpen: PropTypes.bool.isRequired,
firstButtonClick: PropTypes.func.isRequired,
close: PropTypes.func.isRequired
};
const mapDispatchToProps = (dispatch) => {
return {
firstButtonClick: () => dispatch(createFamilyModal()),
close: () => dispatch(closeFamilyModal())
}
};
const mapStateToProps = (state) => {
return {
modalIsOpen: state.modalIsOpen
}
};
LeftMenu = connect(
mapStateToProps,
mapDispatchToProps
)(LeftMenu);
export default LeftMenu
export function createFamilyModal() {
return {
type: CREATE_FAMILY_MODAL
}
}
export function closeFamilyModal() {
return {
type: CLOSE_FAMILY_MODAL
}
}
export function createFamily(familyName, description) {
return {
type: CREATE_FAMILY,
familyName,
description
}
}
\\ updateModel reducer
const updateModal = (state = false, action) => {
switch (action.type) {
case CREATE_FAMILY_MODAL:
return true;
case CLOSE_FAMILY_MODAL:
return false;
default:
return state;
}
};
\\ family reducer
let initialState = {
isEmpty: true,
familyName: '',
description: '',
persons: [],
isFetching: false
};
const family = (state = initialState, action) => {
switch(action.type) {
case CREATE_FAMILY:
return Object.assign({}, state, {
isEmpty: false,
familyName: action.familyName,
description: action.description
});
default:
return state;
}
};
\\ index reducer
const FamilyTreeApp = combineReducers({
family: family,
modalIsOpen: updateModal
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question