Reuse single react component for add and update operation
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
I want to be able to share one single modal component for add and update for a list. I think now my code for the modal is fine, it's stateless:
const MyModal = (
isOpen,
closeModal,
handleChangeDrinkInput,
newDrink,
saveModal,
isEdit
) => (
<Modal isOpen=isOpen onRequestClose=closeModal contentLabel="Modal">
isEdit ? "Edit drink" : "Add drink"
<br />
<input type="text" onChange=handleChangeDrinkInput value=newDrink />
<br />
<br />
<button onClick=() => saveModal(isEdit)>Save</button>
</Modal>
);
export default MyModal;
It has placeholder including event functions coming as props, but I doubt my container is ok, how can I improve the container which is index.js?
https://codesandbox.io/s/l50wrqjmz
For now I'm passing in a flag telling the Modal whether I'm doing add or edit.
saveModal=isEdit =>
if (isEdit)
this.setState(
drinks: this.state.drinks.map(o =>
console.log(this.state.newDrink)
if (o === this.state.oldDrink)
return this.state.newDrink;
return o;
),
modalIsOpen: false,
newDrink: ""
);
else
this.setState(
drinks: [...this.state.drinks, this.state.newDrink],
modalIsOpen: false,
newDrink: ""
);
The problem I see might arise is that if the modal is not simple it might have some more logic, it make the flow not readable.
Full code or demo is here https://codesandbox.io/s/l50wrqjmz
react.js jsx
add a comment |Â
up vote
0
down vote
favorite
I want to be able to share one single modal component for add and update for a list. I think now my code for the modal is fine, it's stateless:
const MyModal = (
isOpen,
closeModal,
handleChangeDrinkInput,
newDrink,
saveModal,
isEdit
) => (
<Modal isOpen=isOpen onRequestClose=closeModal contentLabel="Modal">
isEdit ? "Edit drink" : "Add drink"
<br />
<input type="text" onChange=handleChangeDrinkInput value=newDrink />
<br />
<br />
<button onClick=() => saveModal(isEdit)>Save</button>
</Modal>
);
export default MyModal;
It has placeholder including event functions coming as props, but I doubt my container is ok, how can I improve the container which is index.js?
https://codesandbox.io/s/l50wrqjmz
For now I'm passing in a flag telling the Modal whether I'm doing add or edit.
saveModal=isEdit =>
if (isEdit)
this.setState(
drinks: this.state.drinks.map(o =>
console.log(this.state.newDrink)
if (o === this.state.oldDrink)
return this.state.newDrink;
return o;
),
modalIsOpen: false,
newDrink: ""
);
else
this.setState(
drinks: [...this.state.drinks, this.state.newDrink],
modalIsOpen: false,
newDrink: ""
);
The problem I see might arise is that if the modal is not simple it might have some more logic, it make the flow not readable.
Full code or demo is here https://codesandbox.io/s/l50wrqjmz
react.js jsx
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to be able to share one single modal component for add and update for a list. I think now my code for the modal is fine, it's stateless:
const MyModal = (
isOpen,
closeModal,
handleChangeDrinkInput,
newDrink,
saveModal,
isEdit
) => (
<Modal isOpen=isOpen onRequestClose=closeModal contentLabel="Modal">
isEdit ? "Edit drink" : "Add drink"
<br />
<input type="text" onChange=handleChangeDrinkInput value=newDrink />
<br />
<br />
<button onClick=() => saveModal(isEdit)>Save</button>
</Modal>
);
export default MyModal;
It has placeholder including event functions coming as props, but I doubt my container is ok, how can I improve the container which is index.js?
https://codesandbox.io/s/l50wrqjmz
For now I'm passing in a flag telling the Modal whether I'm doing add or edit.
saveModal=isEdit =>
if (isEdit)
this.setState(
drinks: this.state.drinks.map(o =>
console.log(this.state.newDrink)
if (o === this.state.oldDrink)
return this.state.newDrink;
return o;
),
modalIsOpen: false,
newDrink: ""
);
else
this.setState(
drinks: [...this.state.drinks, this.state.newDrink],
modalIsOpen: false,
newDrink: ""
);
The problem I see might arise is that if the modal is not simple it might have some more logic, it make the flow not readable.
Full code or demo is here https://codesandbox.io/s/l50wrqjmz
react.js jsx
I want to be able to share one single modal component for add and update for a list. I think now my code for the modal is fine, it's stateless:
const MyModal = (
isOpen,
closeModal,
handleChangeDrinkInput,
newDrink,
saveModal,
isEdit
) => (
<Modal isOpen=isOpen onRequestClose=closeModal contentLabel="Modal">
isEdit ? "Edit drink" : "Add drink"
<br />
<input type="text" onChange=handleChangeDrinkInput value=newDrink />
<br />
<br />
<button onClick=() => saveModal(isEdit)>Save</button>
</Modal>
);
export default MyModal;
It has placeholder including event functions coming as props, but I doubt my container is ok, how can I improve the container which is index.js?
https://codesandbox.io/s/l50wrqjmz
For now I'm passing in a flag telling the Modal whether I'm doing add or edit.
saveModal=isEdit =>
if (isEdit)
this.setState(
drinks: this.state.drinks.map(o =>
console.log(this.state.newDrink)
if (o === this.state.oldDrink)
return this.state.newDrink;
return o;
),
modalIsOpen: false,
newDrink: ""
);
else
this.setState(
drinks: [...this.state.drinks, this.state.newDrink],
modalIsOpen: false,
newDrink: ""
);
The problem I see might arise is that if the modal is not simple it might have some more logic, it make the flow not readable.
Full code or demo is here https://codesandbox.io/s/l50wrqjmz
react.js jsx
edited Apr 13 at 16:39
200_success
123k14142399
123k14142399
asked Apr 13 at 8:45
woonkw
11
11
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191950%2freuse-single-react-component-for-add-and-update-operation%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password