Prem
Prem

Reputation: 5997

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

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}
      />
    );
  }
}

Upvotes: 2

Views: 7849

Answers (3)

A Salcin
A Salcin

Reputation: 41

Best way is to actually make your component wrapper around react-select component example:

const CustomSelect = ({onChange, options, value, isMulti}) => {
  return(
       <Select
            options={options}
            onChange={(val) => isMulti
                ? onChange(val.map((c) => c.value))
                : onChange(val.value)
            }
            value={isMulti
                ? options.filter((c) => value.includes(c.value))
                : options.find((c) => c.value === value)
            }
            isMulti={isMulti}
        />
       )
}

Upvotes: 4

аlex
аlex

Reputation: 5698

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}
      />
    );
  }
}

Upvotes: 3

jagadeesh kadavakollu
jagadeesh kadavakollu

Reputation: 148

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}
          />
        );
      }
    }

Upvotes: -4

Related Questions