Default value is not selected with react-select (4.3.1) using React Hooks
See original GitHub issueI 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.
- If the selected value is available in the list then auto selection
- 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:
- Created 2 years ago
- Comments:6
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 theuseEffect
hook is counted as being after the initial render of the component. You should instead use thevalue
prop (in combination with theonChange
prop to handle changes).Also, the value of the two props
value
anddefaultValue
must have exactly the shape that an option in youroptions
prop has (by default, an object consisting of the two propertiesvalue
andlabel
).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