question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default value is not selected with react-select (4.3.1) using React Hooks

See original GitHub issue

I am trying to auto-selected a value from a list of data in the selected component. Kindly help. have already tried with the isLoading flag as well.

  1. If the selected value is available in the list then auto selection
  2. if value not available then no issue.
import React, {useEffect, useState} from 'react';
import Select from 'react-select';
import './App.css';

function App() {
  const [selectCity, setSelectCity] = useState(null);
  const [cityOptions, setCityOptions] = useState([]);

  useEffect(() => {
    setSelectCity("Mumbai");
    setCityOptions([{label: "Kolkata", value:"Kolkata"}, {label: "New Delhi", value:"New Delhi"}, {label: "Chennai", value:"Chennai"}, {label: "Mumbai", value:"Mumbai"}])
  }, []);

  const onCitySelect = (e) => {
    console.log("Selected: ", e);
  };
  return (
    <div className="App">
      <Select
        defaultValue={selectCity}
        options={cityOptions}
        onChange={onCitySelect}
        />
    </div>
  );
}

export default App;

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
Rall3ncommented, May 19, 2021

Hello @ziaul-haque,

I hope you don’t mind that I edited your issue for better readability.

The defaultValue prop is designed for “uncontrolled” behaviour of components (read more about it here). Using it will only change the value for the initial render of the component. Changing a state using the useEffect hook is counted as being after the initial render of the component. You should instead use the value prop (in combination with the onChange prop to handle changes).

Also, the value of the two props value and defaultValue must have exactly the shape that an option in your options prop has (by default, an object consisting of the two properties value and label).

1reaction
alvesheliocommented, May 28, 2021

Hi,

I’m having the same issue and I’m not sure how to handle this. I need to set selected options with data from the backend, what would be the best way to set selected options programatically?

Thank you

Read more comments on GitHub >

github_iconTop Results From Across the Web

Default value is not selected with react-select (4.3.1) using ...
Try to use the value instead of defaultValue props because if you didn't set the default value props then it break your code...
Read more >
react-select default value - CodeSandbox
CodeSandbox is an online editor tailored for web applications. ... Trying to wrangle react-select to select a specific value by default in the...
Read more >
react-select - npm
Component Injection API for complete control over the UI behaviour; Controllable state props and modular architecture; Long-requested features ...
Read more >
Why react-select default value provided but still error ... - GitHub
Why react-select default value provided but still error when required is true.
Read more >
Setting a default value with react-select not working-Reactjs
score:11. Accepted answer. Here the issue is not with state selection, the actual issue is that the label is not getting displayed. ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found