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

Cache doesn't clear when props update #2396

Closed
veltman opened this issue Feb 22, 2018 · 2 comments
Closed

Cache doesn't clear when props update #2396

veltman opened this issue Feb 22, 2018 · 2 comments

Comments

@veltman
Copy link

veltman commented Feb 22, 2018

I've noticed that the default caching behavior for Select.Async is to create a cache object in the constructor, and then mutate that one object forever:

https://github.com/JedWatson/react-select/blob/master/src/Async.js#L56

This means that the cache doesn't clear when the props update, even if the props are changing everything about the input. Here's a simplified reproduction:

https://codepen.io/anon/pen/JpZzzY

  1. The Select autoloads the list of turtles and caches it.
  2. Click on "superheroes", which will update the Select with new props, including a different loadOptions function which should provide a list of superheroes.
  3. Click on the input. You'll see a list of turtles.

I thought the workaround for this would be to include a newly-created cache object as a prop:

<Select.Async cache={{}} loadOptions={fn} ... />

So that whenever the component was re-rendered with new props, it would get a fresh cache object, and if I wanted something persistent I could pass that instead. But this doesn't work, because componentWillReceiveProps doesn't update the internal cache object, it's set forever in the constructor:

https://github.com/JedWatson/react-select/blob/master/src/Async.js#L75-L81

Is the current behavior intentional for some other benefit? If not, it seems like the desired behavior should be to always update the cache in componentWillReceiveProps, along the lines of:

componentWillReceiveProps(nextProps) {
  if (nextProps.cache !== this.props.cache) {
    this._cache = nextProps.cache === defaultCache ? {} : nextProps.cache;
  }
}

Then, if a user uses the default caching behavior, it will have the current behavior, but they at least have the ability to ensure that a cache object they specify as a prop actually becomes the cache on updates and not just during the first construction.

If this makes sense to you I can work up a PR.

@jossmac jossmac added the v1 label Jul 5, 2018
@jossmac
Copy link
Collaborator

jossmac commented Jul 5, 2018

Possibly related #2661, #2582

@flexdinesh
Copy link
Collaborator

Hey @veltman this issue is resolved in v2. v1 of react-select is no longer maintained.

We recommend maintaining your own v1 fork if you want to address v1 issues. We understand this might be inconvenient but in the best interest of supporting the broader community we have to direct our limited efforts to maintain the latest versions.

We highly recommend upgrading from v1 to v2.

I'm closing this issue/PR now. Feel free add your thoughts in #3981 and reference this issue/PR if needed.

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

3 participants