How to react-select set value from state

    0

    0

    Jose Romero

    import { useState } from "react";
    import Select from "react-select";
    
    const options = [
      { value: "A", label: "A" },
      { value: "B", label: "B" }
    ];
    export default function App() {
      const [value, setValue] = useState("A");
      const handleChange = (value) => {
        setValue(value);    // here you set state value
      };
      return (
        <div className="App">
          <Select
            name="form-field-name"
            value={value}
            onChange={handleChange}
            options={options}
          />
          <button onClick={() => setValue(null)}>reset</button>
        </div>
      );
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.