Component Options do not update when the state changes
See original GitHub issueDescribe the bug: When options state is updated, the dropdown do not show the new value.
To Reproduce:
import React, { useState } from "react";
import MultiSelect from "react-multi-select-component";
import "./styles.css";
export default function App() {
const [selected, setSelected] = useState([]);
const [options, setOptions] = useState([
{ label: "Grapes 🍇", value: "grapes" }
]);
const AddOption = () => {
setOptions([ { label: "Mango 🥭", value: "mango" },
{ label: "Strawberry 🍓", value: "strawberry" }
]);
};
return (
<div>
<h1>Select Fruits</h1>
<p>{JSON.stringify(options)}</p>
<MultiSelect
options={options}
value={selected}
onChange={setSelected}
labelledBy={"Select"}
/>
<button onClick={AddOption}> Add option </button>
</div>
);
}
This only happens from version 4.0.2 onwards. No issue with version 4.0.1
Sample Code using Version 4.0.3 https://codesandbox.io/s/react-kerl-bug-0hm06?file=/src/App.js
Desired behaviour: When Option state changes, component should reflect the state just like in version 4.0.1
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
React Component not updating with state change
To fix, either remove this component state entirely and just connect it to the redux store, or update the component state onChange. I...
Read more >Why React doesn't update state immediately - LogRocket Blog
When developing React applications, you may have noticed that state updates don't immediately reflect new values after being changed.
Read more >Component not updating when changing a element in a array ...
When you create a array using useState() , and then you update one of it's elements, the component will not update. You can...
Read more >You Might Not Need an Effect - React Docs
You might feel tempted to write an Effect that updates a state variable when the list changes. However, this is inefficient. When you...
Read more >When does React re-render components? - Felix Gerschau
Changing the state means that React triggers an update when we call the setState function (in React hooks, you would use useState )....
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
@harshzalavadiya thank you so much.
Hi @harshzalavadiya ,
When page first load, the Option only have Grape:
Picture above depicts the Option state that only contains “Grape” using version 4.0.3
To simplified my scenario,i wrote a function to overwrite the Option to the following:
"[{"label":"Mango 🥭","value":"mango"},{"label":"Strawberry 🍓","value":"strawberry"}]"
Unfortunately, the dropdown component do not reflect the new values after i tried to add into the Option state Picture above depicts the Option state been replace with Mango and strawberry but the Dropdown component still show Grape using version 4.0.3
Below is the picture when i change the version to 4.0.1 that work.