Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Options does not update from state/props for functional components #134

Closed
abhaykumar01234 opened this issue Oct 29, 2020 · 6 comments
Closed

Comments

@abhaykumar01234
Copy link

abhaykumar01234 commented Oct 29, 2020

import React, { useState, useEffect } from "react";
import Select from "react-dropdown-select";

const Try = () => {
const [options, setOptions] = useState([]);
const [loadedNow, setLoadedNow] = useState("users");

useEffect(() => {
changeToUsers();
}, []);

const changeToPosts = () => {
const result = [
{ title: "Post1", desc: "post1 description" },
{ title: "Post2", desc: "post2 description" },
];

setOptions(
result.map((post) => ({
label: post.title,
value: post.desc,
}))
);

setLoadedNow("posts");
};

const changeToUsers = () => {
const result = [
{ username: "Abhay", email: "abhay@gmail.co" },
{ username: "Ajay", email: "ajay@gmail.co" },
];

setOptions(
result.map((user) => ({
label: user.username,
value: user.email,
}))
);

setLoadedNow("users");
};
return (
<div style={{ padding: "1rem", background: "#fff" }}>
<h1>Try page</h1>
<hr />
<br />
<div>
<p>Hello | {loadedNow} loaded</p>
{loadedNow === "users" && (
<button onClick={changeToPosts}>change to posts</button>
)}
{loadedNow === "posts" && (
<button onClick={changeToUsers}>change to users</button>
)}
</div>

<Select
placeholder="Type to search"
multi
// loading={this.state.loading}
onChange={() => undefined}
values={[]}
keepOpen
options={options}
/>
</div>
);
};

export default Try;

Not working. Please provide solution

@sanusart
Copy link
Owner

ChangeToPosts and ChangeToUsers never return

@abhaykumar01234
Copy link
Author

abhaykumar01234 commented Oct 29, 2020

ChangeToPosts and ChangeToUsers never return

@sanusart
I didn't get you. What should these functions return?
They are already calling the setter function

I looked up previous issues and tried to implement your example
https://codesandbox.io/s/epic-herschel-194rp?file=/src/App.js
for synchronous values

inside a functional component

@abhaykumar01234
Copy link
Author

ChangeToPosts and ChangeToUsers never return

https://codesandbox.io/s/epic-herschel-194rp?file=/src/App.js
Can you share a similar example sample code for functional components?

@sanusart
Copy link
Owner

@abhaykumar01234
Copy link
Author

@abhaykumar01234
https://codesandbox.io/s/vibrant-https-zg90n?file=/src/App.js

@sanusart
I noticed that it works for version 4.5.1 but not for version 4.6.1
[ created a new react 17.0.1 app to test] / [ also compatible with react 16.14 and older versions]

So i downgraded to 4.5.1 for now.

Please do check for 4.6.1. And thanks for the prompt replies.

@sanusart
Copy link
Owner

Yes. regression was introduced in 4.6.0. #133 Thanks. Latest version with no issue 4.5.2

sanusart pushed a commit that referenced this issue Nov 17, 2020
…ing in the search bar, #134, #133 (#136)

* Working searchFn example.

* Remove debug code.

* Undo indentation

* Restore changes.

* Remove debug code.

* Restore return.

* Add test for a custom function.

* Add test coverage for custom search function.

* Apply code review suggestions.

* Reset searchResults on blur.

* Clear search results on dropdown; update search results on options change.
sanusart added a commit that referenced this issue Nov 29, 2020
* master: (27 commits)
  v4.7.0 See changelog: https://github.com/sanusart/react-dropdown-select/blob/master/CHANGELOG.md
  FIX (searchFn)[#101] searchFn callback should only call once when typing in the search bar, #134, #133 (#136)
  4.6.1
  Revert "FIX (searchFn)[#101] searchFn callback should only call once when typing in the search bar (#131)" (#135)
  v4.6.0 See changelog: https://github.com/sanusart/react-dropdown-select/blob/master/CHANGELOG.md
  FIX (searchFn)[#101] searchFn callback should only call once when typing in the search bar (#131)
  v4.5.2 See changelog: https://github.com/sanusart/react-dropdown-select/blob/master/CHANGELOG.md
  FIX (style): Accepts a short color (#127)
  v4.5.1 See changelog: https://github.com/sanusart/react-dropdown-select/blob/master/CHANGELOG.md
  FIX (duplicates)[#116]: more proper way to check dupes, closes #116
  FIX (duplicates): PR comments
  FIX (duplicates)[#116]: more proper way to check dupes, closes #116
  bump version
  v4.5.0 See changelog: https://github.com/sanusart/react-dropdown-select/blob/master/CHANGELOG.md
  FIX (input) [#114]: fix chars truncated, closes #114
  v4.4.2 See changelog: https://github.com/sanusart/react-dropdown-select/blob/master/CHANGELOG.md
  FIX (box-model): add box-sizing border-box, closes #94 (#104)
  Bump websocket-extensions from 0.1.3 to 0.1.4 (#102)
  DOCS (example): add import to example, thanks to @karland, closes #93
  CHORE (types): fix keepOpen type | closes #95 (#96)
  ...

# Conflicts:
#	__tests__/__snapshots__/index.spec.js.snap
#	__tests__/components/__snapshots__/DropdownHandle.spec.js.snap
#	package-lock.json
#	src/components/DropdownHandle.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants