How to set & get only value string in react-select's state












0















I have tried following code, but it sets & gets the entire json of the selected value. How to set & get only the value of the selectedOption & not the entire json of the selectedOption from state after the selection. The code given in the the official documentation is given below:



import React from 'react';
import Select from 'react-select';

const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];

class App extends React.Component {
state = {
selectedOption: null,
}
handleChange = (selectedOption) => {
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
}
render() {
const { selectedOption } = this.state;

return (
<Select
value={selectedOption}
onChange={this.handleChange}
options={options}
/>
);
}
}









share|improve this question























  • could you please show us what console.log('Option selected: ' + JSON.stringify(selectedOption)); prints?

    – c-chavez
    Nov 23 '18 at 10:14











  • @c-chavez if all the three options are selected, then it prints all three options: [ { value: 'chocolate', label: 'Chocolate' }, { value: 'strawberry', label: 'Strawberry' }, { value: 'vanilla', label: 'Vanilla' } ]

    – Prem
    Nov 23 '18 at 10:24











  • how can 3 options be selected... there should only be one option selected... I'm still missing something with your question. Could you please edit your question and explain a bit more what you need, what is currently happening, and what do you expect from your output?

    – c-chavez
    Nov 23 '18 at 12:46











  • there is a option called as multiselect available in the package

    – Prem
    Nov 23 '18 at 12:49











  • So, when you have 3 options selected what do you want to show? what is your expected output? If you have a read again at your question, you never mention this... it is different to handle just one object than to handle an array of objects.

    – c-chavez
    Nov 23 '18 at 15:54
















0















I have tried following code, but it sets & gets the entire json of the selected value. How to set & get only the value of the selectedOption & not the entire json of the selectedOption from state after the selection. The code given in the the official documentation is given below:



import React from 'react';
import Select from 'react-select';

const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];

class App extends React.Component {
state = {
selectedOption: null,
}
handleChange = (selectedOption) => {
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
}
render() {
const { selectedOption } = this.state;

return (
<Select
value={selectedOption}
onChange={this.handleChange}
options={options}
/>
);
}
}









share|improve this question























  • could you please show us what console.log('Option selected: ' + JSON.stringify(selectedOption)); prints?

    – c-chavez
    Nov 23 '18 at 10:14











  • @c-chavez if all the three options are selected, then it prints all three options: [ { value: 'chocolate', label: 'Chocolate' }, { value: 'strawberry', label: 'Strawberry' }, { value: 'vanilla', label: 'Vanilla' } ]

    – Prem
    Nov 23 '18 at 10:24











  • how can 3 options be selected... there should only be one option selected... I'm still missing something with your question. Could you please edit your question and explain a bit more what you need, what is currently happening, and what do you expect from your output?

    – c-chavez
    Nov 23 '18 at 12:46











  • there is a option called as multiselect available in the package

    – Prem
    Nov 23 '18 at 12:49











  • So, when you have 3 options selected what do you want to show? what is your expected output? If you have a read again at your question, you never mention this... it is different to handle just one object than to handle an array of objects.

    – c-chavez
    Nov 23 '18 at 15:54














0












0








0








I have tried following code, but it sets & gets the entire json of the selected value. How to set & get only the value of the selectedOption & not the entire json of the selectedOption from state after the selection. The code given in the the official documentation is given below:



import React from 'react';
import Select from 'react-select';

const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];

class App extends React.Component {
state = {
selectedOption: null,
}
handleChange = (selectedOption) => {
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
}
render() {
const { selectedOption } = this.state;

return (
<Select
value={selectedOption}
onChange={this.handleChange}
options={options}
/>
);
}
}









share|improve this question














I have tried following code, but it sets & gets the entire json of the selected value. How to set & get only the value of the selectedOption & not the entire json of the selectedOption from state after the selection. The code given in the the official documentation is given below:



import React from 'react';
import Select from 'react-select';

const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];

class App extends React.Component {
state = {
selectedOption: null,
}
handleChange = (selectedOption) => {
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
}
render() {
const { selectedOption } = this.state;

return (
<Select
value={selectedOption}
onChange={this.handleChange}
options={options}
/>
);
}
}






reactjs react-select






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 10:00









PremPrem

1,70382653




1,70382653













  • could you please show us what console.log('Option selected: ' + JSON.stringify(selectedOption)); prints?

    – c-chavez
    Nov 23 '18 at 10:14











  • @c-chavez if all the three options are selected, then it prints all three options: [ { value: 'chocolate', label: 'Chocolate' }, { value: 'strawberry', label: 'Strawberry' }, { value: 'vanilla', label: 'Vanilla' } ]

    – Prem
    Nov 23 '18 at 10:24











  • how can 3 options be selected... there should only be one option selected... I'm still missing something with your question. Could you please edit your question and explain a bit more what you need, what is currently happening, and what do you expect from your output?

    – c-chavez
    Nov 23 '18 at 12:46











  • there is a option called as multiselect available in the package

    – Prem
    Nov 23 '18 at 12:49











  • So, when you have 3 options selected what do you want to show? what is your expected output? If you have a read again at your question, you never mention this... it is different to handle just one object than to handle an array of objects.

    – c-chavez
    Nov 23 '18 at 15:54



















  • could you please show us what console.log('Option selected: ' + JSON.stringify(selectedOption)); prints?

    – c-chavez
    Nov 23 '18 at 10:14











  • @c-chavez if all the three options are selected, then it prints all three options: [ { value: 'chocolate', label: 'Chocolate' }, { value: 'strawberry', label: 'Strawberry' }, { value: 'vanilla', label: 'Vanilla' } ]

    – Prem
    Nov 23 '18 at 10:24











  • how can 3 options be selected... there should only be one option selected... I'm still missing something with your question. Could you please edit your question and explain a bit more what you need, what is currently happening, and what do you expect from your output?

    – c-chavez
    Nov 23 '18 at 12:46











  • there is a option called as multiselect available in the package

    – Prem
    Nov 23 '18 at 12:49











  • So, when you have 3 options selected what do you want to show? what is your expected output? If you have a read again at your question, you never mention this... it is different to handle just one object than to handle an array of objects.

    – c-chavez
    Nov 23 '18 at 15:54

















could you please show us what console.log('Option selected: ' + JSON.stringify(selectedOption)); prints?

– c-chavez
Nov 23 '18 at 10:14





could you please show us what console.log('Option selected: ' + JSON.stringify(selectedOption)); prints?

– c-chavez
Nov 23 '18 at 10:14













@c-chavez if all the three options are selected, then it prints all three options: [ { value: 'chocolate', label: 'Chocolate' }, { value: 'strawberry', label: 'Strawberry' }, { value: 'vanilla', label: 'Vanilla' } ]

– Prem
Nov 23 '18 at 10:24





@c-chavez if all the three options are selected, then it prints all three options: [ { value: 'chocolate', label: 'Chocolate' }, { value: 'strawberry', label: 'Strawberry' }, { value: 'vanilla', label: 'Vanilla' } ]

– Prem
Nov 23 '18 at 10:24













how can 3 options be selected... there should only be one option selected... I'm still missing something with your question. Could you please edit your question and explain a bit more what you need, what is currently happening, and what do you expect from your output?

– c-chavez
Nov 23 '18 at 12:46





how can 3 options be selected... there should only be one option selected... I'm still missing something with your question. Could you please edit your question and explain a bit more what you need, what is currently happening, and what do you expect from your output?

– c-chavez
Nov 23 '18 at 12:46













there is a option called as multiselect available in the package

– Prem
Nov 23 '18 at 12:49





there is a option called as multiselect available in the package

– Prem
Nov 23 '18 at 12:49













So, when you have 3 options selected what do you want to show? what is your expected output? If you have a read again at your question, you never mention this... it is different to handle just one object than to handle an array of objects.

– c-chavez
Nov 23 '18 at 15:54





So, when you have 3 options selected what do you want to show? what is your expected output? If you have a read again at your question, you never mention this... it is different to handle just one object than to handle an array of objects.

– c-chavez
Nov 23 '18 at 15:54












2 Answers
2






active

oldest

votes


















0














you should bind handleChange in constructor



because react-reselect by default bind all methods in code line https://github.com/JedWatson/react-select/blob/master/.babelrc#L4



transform-class-properties in .babelrc bind by default



Do bind like this:
https://medium.freecodecamp.org/react-binding-patterns-5-approaches-for-handling-this-92c651b5af56



And the answer to your question here:



import React from 'react';
import Select from 'react-select';

const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];

class App extends React.Component {
state = {
selectedOption: null,
}
constructor() {
this.handleChange = this.handleChange.bind(this)
}
handleChange = (selectedOption) => {
console.log('selectedOption', selectedOption); // log here before set state
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
}
render() {
const { selectedOption } = this.state;

return (
<Select
value={selectedOption}
onChange={this.handleChange}
options={options}
/>
);
}
}





share|improve this answer
























  • arrow functions doesnt need bindings

    – Prem
    Nov 23 '18 at 12:27



















-2














Extract selected value and pass to handler



   onChange={(e) => this.handleChange(e.value)}


...



    import React from 'react';
import Select from 'react-select';

const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];

class App extends React.Component {
state = {
selectedOption: null,
}
handleChange = (selectedOption) => {
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
}
render() {
const { selectedOption } = this.state;

return (
<Select
value={selectedOption}
onChange={(e) => this.handleChange(e.value)}
options={options}
/>
);
}
}





share|improve this answer


























  • please explain what your code does, what you have changed, so that OP and the rest of the users can understand why is this an answer to the question.

    – c-chavez
    Nov 23 '18 at 10:13











  • Please edit your answer and add this info, reading the comments is harder for future viewers.

    – c-chavez
    Nov 23 '18 at 10:22











  • What's wrong with the answer?? Please aware of what you are posting and expecting :)

    – jagadeesh kadavakollu
    Nov 23 '18 at 13:14












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444424%2fhow-to-set-get-only-value-string-in-react-selects-state%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














you should bind handleChange in constructor



because react-reselect by default bind all methods in code line https://github.com/JedWatson/react-select/blob/master/.babelrc#L4



transform-class-properties in .babelrc bind by default



Do bind like this:
https://medium.freecodecamp.org/react-binding-patterns-5-approaches-for-handling-this-92c651b5af56



And the answer to your question here:



import React from 'react';
import Select from 'react-select';

const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];

class App extends React.Component {
state = {
selectedOption: null,
}
constructor() {
this.handleChange = this.handleChange.bind(this)
}
handleChange = (selectedOption) => {
console.log('selectedOption', selectedOption); // log here before set state
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
}
render() {
const { selectedOption } = this.state;

return (
<Select
value={selectedOption}
onChange={this.handleChange}
options={options}
/>
);
}
}





share|improve this answer
























  • arrow functions doesnt need bindings

    – Prem
    Nov 23 '18 at 12:27
















0














you should bind handleChange in constructor



because react-reselect by default bind all methods in code line https://github.com/JedWatson/react-select/blob/master/.babelrc#L4



transform-class-properties in .babelrc bind by default



Do bind like this:
https://medium.freecodecamp.org/react-binding-patterns-5-approaches-for-handling-this-92c651b5af56



And the answer to your question here:



import React from 'react';
import Select from 'react-select';

const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];

class App extends React.Component {
state = {
selectedOption: null,
}
constructor() {
this.handleChange = this.handleChange.bind(this)
}
handleChange = (selectedOption) => {
console.log('selectedOption', selectedOption); // log here before set state
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
}
render() {
const { selectedOption } = this.state;

return (
<Select
value={selectedOption}
onChange={this.handleChange}
options={options}
/>
);
}
}





share|improve this answer
























  • arrow functions doesnt need bindings

    – Prem
    Nov 23 '18 at 12:27














0












0








0







you should bind handleChange in constructor



because react-reselect by default bind all methods in code line https://github.com/JedWatson/react-select/blob/master/.babelrc#L4



transform-class-properties in .babelrc bind by default



Do bind like this:
https://medium.freecodecamp.org/react-binding-patterns-5-approaches-for-handling-this-92c651b5af56



And the answer to your question here:



import React from 'react';
import Select from 'react-select';

const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];

class App extends React.Component {
state = {
selectedOption: null,
}
constructor() {
this.handleChange = this.handleChange.bind(this)
}
handleChange = (selectedOption) => {
console.log('selectedOption', selectedOption); // log here before set state
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
}
render() {
const { selectedOption } = this.state;

return (
<Select
value={selectedOption}
onChange={this.handleChange}
options={options}
/>
);
}
}





share|improve this answer













you should bind handleChange in constructor



because react-reselect by default bind all methods in code line https://github.com/JedWatson/react-select/blob/master/.babelrc#L4



transform-class-properties in .babelrc bind by default



Do bind like this:
https://medium.freecodecamp.org/react-binding-patterns-5-approaches-for-handling-this-92c651b5af56



And the answer to your question here:



import React from 'react';
import Select from 'react-select';

const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];

class App extends React.Component {
state = {
selectedOption: null,
}
constructor() {
this.handleChange = this.handleChange.bind(this)
}
handleChange = (selectedOption) => {
console.log('selectedOption', selectedOption); // log here before set state
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
}
render() {
const { selectedOption } = this.state;

return (
<Select
value={selectedOption}
onChange={this.handleChange}
options={options}
/>
);
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 10:28









аlex dykyіаlex dykyі

2,0461327




2,0461327













  • arrow functions doesnt need bindings

    – Prem
    Nov 23 '18 at 12:27



















  • arrow functions doesnt need bindings

    – Prem
    Nov 23 '18 at 12:27

















arrow functions doesnt need bindings

– Prem
Nov 23 '18 at 12:27





arrow functions doesnt need bindings

– Prem
Nov 23 '18 at 12:27













-2














Extract selected value and pass to handler



   onChange={(e) => this.handleChange(e.value)}


...



    import React from 'react';
import Select from 'react-select';

const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];

class App extends React.Component {
state = {
selectedOption: null,
}
handleChange = (selectedOption) => {
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
}
render() {
const { selectedOption } = this.state;

return (
<Select
value={selectedOption}
onChange={(e) => this.handleChange(e.value)}
options={options}
/>
);
}
}





share|improve this answer


























  • please explain what your code does, what you have changed, so that OP and the rest of the users can understand why is this an answer to the question.

    – c-chavez
    Nov 23 '18 at 10:13











  • Please edit your answer and add this info, reading the comments is harder for future viewers.

    – c-chavez
    Nov 23 '18 at 10:22











  • What's wrong with the answer?? Please aware of what you are posting and expecting :)

    – jagadeesh kadavakollu
    Nov 23 '18 at 13:14
















-2














Extract selected value and pass to handler



   onChange={(e) => this.handleChange(e.value)}


...



    import React from 'react';
import Select from 'react-select';

const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];

class App extends React.Component {
state = {
selectedOption: null,
}
handleChange = (selectedOption) => {
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
}
render() {
const { selectedOption } = this.state;

return (
<Select
value={selectedOption}
onChange={(e) => this.handleChange(e.value)}
options={options}
/>
);
}
}





share|improve this answer


























  • please explain what your code does, what you have changed, so that OP and the rest of the users can understand why is this an answer to the question.

    – c-chavez
    Nov 23 '18 at 10:13











  • Please edit your answer and add this info, reading the comments is harder for future viewers.

    – c-chavez
    Nov 23 '18 at 10:22











  • What's wrong with the answer?? Please aware of what you are posting and expecting :)

    – jagadeesh kadavakollu
    Nov 23 '18 at 13:14














-2












-2








-2







Extract selected value and pass to handler



   onChange={(e) => this.handleChange(e.value)}


...



    import React from 'react';
import Select from 'react-select';

const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];

class App extends React.Component {
state = {
selectedOption: null,
}
handleChange = (selectedOption) => {
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
}
render() {
const { selectedOption } = this.state;

return (
<Select
value={selectedOption}
onChange={(e) => this.handleChange(e.value)}
options={options}
/>
);
}
}





share|improve this answer















Extract selected value and pass to handler



   onChange={(e) => this.handleChange(e.value)}


...



    import React from 'react';
import Select from 'react-select';

const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];

class App extends React.Component {
state = {
selectedOption: null,
}
handleChange = (selectedOption) => {
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
}
render() {
const { selectedOption } = this.state;

return (
<Select
value={selectedOption}
onChange={(e) => this.handleChange(e.value)}
options={options}
/>
);
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 10:24

























answered Nov 23 '18 at 10:12









jagadeesh kadavakollujagadeesh kadavakollu

1024




1024













  • please explain what your code does, what you have changed, so that OP and the rest of the users can understand why is this an answer to the question.

    – c-chavez
    Nov 23 '18 at 10:13











  • Please edit your answer and add this info, reading the comments is harder for future viewers.

    – c-chavez
    Nov 23 '18 at 10:22











  • What's wrong with the answer?? Please aware of what you are posting and expecting :)

    – jagadeesh kadavakollu
    Nov 23 '18 at 13:14



















  • please explain what your code does, what you have changed, so that OP and the rest of the users can understand why is this an answer to the question.

    – c-chavez
    Nov 23 '18 at 10:13











  • Please edit your answer and add this info, reading the comments is harder for future viewers.

    – c-chavez
    Nov 23 '18 at 10:22











  • What's wrong with the answer?? Please aware of what you are posting and expecting :)

    – jagadeesh kadavakollu
    Nov 23 '18 at 13:14

















please explain what your code does, what you have changed, so that OP and the rest of the users can understand why is this an answer to the question.

– c-chavez
Nov 23 '18 at 10:13





please explain what your code does, what you have changed, so that OP and the rest of the users can understand why is this an answer to the question.

– c-chavez
Nov 23 '18 at 10:13













Please edit your answer and add this info, reading the comments is harder for future viewers.

– c-chavez
Nov 23 '18 at 10:22





Please edit your answer and add this info, reading the comments is harder for future viewers.

– c-chavez
Nov 23 '18 at 10:22













What's wrong with the answer?? Please aware of what you are posting and expecting :)

– jagadeesh kadavakollu
Nov 23 '18 at 13:14





What's wrong with the answer?? Please aware of what you are posting and expecting :)

– jagadeesh kadavakollu
Nov 23 '18 at 13:14


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444424%2fhow-to-set-get-only-value-string-in-react-selects-state%23new-answer', 'question_page');
}
);

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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini