How to update nested array (multi react-select)
up vote
2
down vote
favorite
I'm creating an app where I need to store selected values in array nested in object (category below). State looks like:
state =
data:
user: "",
title: "",
text: "",
category: , // should store values
,
updateNoteId: null,
In my render() I have following form:
<form onSubmit=this.submitNote>
<Select
name="category"
value=this.state.data.category
options=options
onChange=this.handleMultiChange
multi
/>
<input type="submit" value="Save" />
</form>
Options are:
const options = [
value: 1, label: 'one' ,
value: 2, label: 'two' ,
value: 3, label: 'three'
]
So the question is how this.handleMultiChange
function should looks like to work. Category need to keep all values selected in Select which is react-select component (eg. it should be category = [1,3], when 'one' and 'three' were chosen). I tried many combination but none of them worked so far. I prefer to use ES6 without any external libraries/helpers to do that.
javascript reactjs ecmascript-6 react-select
add a comment |
up vote
2
down vote
favorite
I'm creating an app where I need to store selected values in array nested in object (category below). State looks like:
state =
data:
user: "",
title: "",
text: "",
category: , // should store values
,
updateNoteId: null,
In my render() I have following form:
<form onSubmit=this.submitNote>
<Select
name="category"
value=this.state.data.category
options=options
onChange=this.handleMultiChange
multi
/>
<input type="submit" value="Save" />
</form>
Options are:
const options = [
value: 1, label: 'one' ,
value: 2, label: 'two' ,
value: 3, label: 'three'
]
So the question is how this.handleMultiChange
function should looks like to work. Category need to keep all values selected in Select which is react-select component (eg. it should be category = [1,3], when 'one' and 'three' were chosen). I tried many combination but none of them worked so far. I prefer to use ES6 without any external libraries/helpers to do that.
javascript reactjs ecmascript-6 react-select
Show what you have tried as a Minimal, Complete, and Verifiable example along with an explanation of your results.
– jdv
Nov 8 at 17:09
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm creating an app where I need to store selected values in array nested in object (category below). State looks like:
state =
data:
user: "",
title: "",
text: "",
category: , // should store values
,
updateNoteId: null,
In my render() I have following form:
<form onSubmit=this.submitNote>
<Select
name="category"
value=this.state.data.category
options=options
onChange=this.handleMultiChange
multi
/>
<input type="submit" value="Save" />
</form>
Options are:
const options = [
value: 1, label: 'one' ,
value: 2, label: 'two' ,
value: 3, label: 'three'
]
So the question is how this.handleMultiChange
function should looks like to work. Category need to keep all values selected in Select which is react-select component (eg. it should be category = [1,3], when 'one' and 'three' were chosen). I tried many combination but none of them worked so far. I prefer to use ES6 without any external libraries/helpers to do that.
javascript reactjs ecmascript-6 react-select
I'm creating an app where I need to store selected values in array nested in object (category below). State looks like:
state =
data:
user: "",
title: "",
text: "",
category: , // should store values
,
updateNoteId: null,
In my render() I have following form:
<form onSubmit=this.submitNote>
<Select
name="category"
value=this.state.data.category
options=options
onChange=this.handleMultiChange
multi
/>
<input type="submit" value="Save" />
</form>
Options are:
const options = [
value: 1, label: 'one' ,
value: 2, label: 'two' ,
value: 3, label: 'three'
]
So the question is how this.handleMultiChange
function should looks like to work. Category need to keep all values selected in Select which is react-select component (eg. it should be category = [1,3], when 'one' and 'three' were chosen). I tried many combination but none of them worked so far. I prefer to use ES6 without any external libraries/helpers to do that.
javascript reactjs ecmascript-6 react-select
javascript reactjs ecmascript-6 react-select
edited Nov 8 at 17:35
chazsolo
4,8921232
4,8921232
asked Nov 8 at 17:03
rafalm
111
111
Show what you have tried as a Minimal, Complete, and Verifiable example along with an explanation of your results.
– jdv
Nov 8 at 17:09
add a comment |
Show what you have tried as a Minimal, Complete, and Verifiable example along with an explanation of your results.
– jdv
Nov 8 at 17:09
Show what you have tried as a Minimal, Complete, and Verifiable example along with an explanation of your results.
– jdv
Nov 8 at 17:09
Show what you have tried as a Minimal, Complete, and Verifiable example along with an explanation of your results.
– jdv
Nov 8 at 17:09
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
handleMultiChange(selectedOptions)
this.setState(
data:
...this.state.data,
categories: selectedOptions
)
Thanks for response, but it's not working. Console show undefined values and Select seems also to work incorrect... I've prepared codesandbox -> (codesandbox.io/s/6108wxlqxn)
– rafalm
Nov 8 at 18:43
Sorry, I didn't know you was using React-Select. Working Code Sandbox - codesandbox.io/s/502qv3qp4k I've also updated my answer
– Charlie
Nov 9 at 10:37
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
handleMultiChange(selectedOptions)
this.setState(
data:
...this.state.data,
categories: selectedOptions
)
Thanks for response, but it's not working. Console show undefined values and Select seems also to work incorrect... I've prepared codesandbox -> (codesandbox.io/s/6108wxlqxn)
– rafalm
Nov 8 at 18:43
Sorry, I didn't know you was using React-Select. Working Code Sandbox - codesandbox.io/s/502qv3qp4k I've also updated my answer
– Charlie
Nov 9 at 10:37
add a comment |
up vote
2
down vote
handleMultiChange(selectedOptions)
this.setState(
data:
...this.state.data,
categories: selectedOptions
)
Thanks for response, but it's not working. Console show undefined values and Select seems also to work incorrect... I've prepared codesandbox -> (codesandbox.io/s/6108wxlqxn)
– rafalm
Nov 8 at 18:43
Sorry, I didn't know you was using React-Select. Working Code Sandbox - codesandbox.io/s/502qv3qp4k I've also updated my answer
– Charlie
Nov 9 at 10:37
add a comment |
up vote
2
down vote
up vote
2
down vote
handleMultiChange(selectedOptions)
this.setState(
data:
...this.state.data,
categories: selectedOptions
)
handleMultiChange(selectedOptions)
this.setState(
data:
...this.state.data,
categories: selectedOptions
)
edited Nov 9 at 15:30
answered Nov 8 at 17:17
Charlie
1296
1296
Thanks for response, but it's not working. Console show undefined values and Select seems also to work incorrect... I've prepared codesandbox -> (codesandbox.io/s/6108wxlqxn)
– rafalm
Nov 8 at 18:43
Sorry, I didn't know you was using React-Select. Working Code Sandbox - codesandbox.io/s/502qv3qp4k I've also updated my answer
– Charlie
Nov 9 at 10:37
add a comment |
Thanks for response, but it's not working. Console show undefined values and Select seems also to work incorrect... I've prepared codesandbox -> (codesandbox.io/s/6108wxlqxn)
– rafalm
Nov 8 at 18:43
Sorry, I didn't know you was using React-Select. Working Code Sandbox - codesandbox.io/s/502qv3qp4k I've also updated my answer
– Charlie
Nov 9 at 10:37
Thanks for response, but it's not working. Console show undefined values and Select seems also to work incorrect... I've prepared codesandbox -> (codesandbox.io/s/6108wxlqxn)
– rafalm
Nov 8 at 18:43
Thanks for response, but it's not working. Console show undefined values and Select seems also to work incorrect... I've prepared codesandbox -> (codesandbox.io/s/6108wxlqxn)
– rafalm
Nov 8 at 18:43
Sorry, I didn't know you was using React-Select. Working Code Sandbox - codesandbox.io/s/502qv3qp4k I've also updated my answer
– Charlie
Nov 9 at 10:37
Sorry, I didn't know you was using React-Select. Working Code Sandbox - codesandbox.io/s/502qv3qp4k I've also updated my answer
– Charlie
Nov 9 at 10:37
add a comment |
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212729%2fhow-to-update-nested-array-multi-react-select%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Show what you have tried as a Minimal, Complete, and Verifiable example along with an explanation of your results.
– jdv
Nov 8 at 17:09