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.

Component Options do not update when the state changes

See original GitHub issue

Describe 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:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
wongCHcommented, Aug 2, 2021

@harshzalavadiya thank you so much.

1reaction
wongCHcommented, Aug 2, 2021

Hi @harshzalavadiya ,

When page first load, the Option only have Grape:

Before

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 After 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.

correct
Read more comments on GitHub >

github_iconTop 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 >

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