react-select icon indicating copy to clipboard operation
react-select copied to clipboard

How to force reload of options?

Open obykoo opened this issue 9 years ago • 38 comments

I want to reload default async options , when use enter some data in other part of app,but I do not know how to force reload of options.

obykoo avatar Mar 01 '17 01:03 obykoo

+1

Luke1298 avatar Mar 25 '17 19:03 Luke1298

Any update on this?

keehun avatar Jun 23 '17 16:06 keehun

I have a dropdown for country which then determines the Async.Select "options".

Change function for the country select reloads the options for the Async location select.

_handleChange_country() {
  if (this.refs.country.value != this.state.country) {
    this.setState({
      country: this.refs.country.value,
      location_value: []
    })
    this.refs.bob.loadOptions('')
  }
}`

//// Inside render()
<select
  ref="country"
  value={this.state.country}
  onChange={this._handleChange_country}>
.....snip options...
</select>

<Select.Async
  ref="bob"
  multi={true}
  placeholder={this.props.search_placeholder}
  value={this.state.location_value}
  onChange={this._handleChange_location}
  loadOptions={this._getLocationOptions.bind(this)}
  valueKey='value'
  labelKey='label'
  autoload={false}
  cache={false}
>

aarmel-sw avatar Sep 25 '17 22:09 aarmel-sw

Apart from hacking with reference, is there a cleaner / built-in way to handle a refresh?

MacKentoch avatar Oct 26 '17 13:10 MacKentoch

I'm not aware of any way at the moment. What is being asked for is a way to force a refresh of the options based on something else in an application changing (which could be a lot of things).

Only option I can think of is to have a listener of some sore on that something that then calls this.refs.bob.loadOptions('') to refresh bob's options. Or inbuilt into the Select.Async application to run "loadOptions('')" when it receives a new prop. That's if anyone out there wants to try and modify Select.Async to have a refresh options option.

aarmel-sw avatar Oct 26 '17 20:10 aarmel-sw

Any update on this?

kevingilbert100 avatar Feb 25 '18 22:02 kevingilbert100

any update on this?

adnanfajlur avatar Jul 19 '18 11:07 adnanfajlur

With the latest version.

I have a react component that use AsyncSelect, latest version has changed a bit.

In this I have a clearValues function. In the component that has all the other form controls, I call the clear values function when I need to reset the values of the Async Select.

  _handleChange_country(event) {
      this.refs.async_location_select.clearValues()

      this.setState({
        country_eq: this.refs.country.value,
      })
  }
  clearValues = () => {
    this.refs.async.select.state.value = ''
  }


In the render AsnycSelect_Location_Component

     <AsyncSelect
        ref              = 'async'
        noOptionsMessage = { () => 'Type in location' }
        styles           = { customStyles }
        placeholder      = 'Enter city, state or postcode'
        isMulti
        cacheOptions
        loadOptions      = { this.loadOptions }
        defaultOptions   = { false }
        onInputChange    = { this.handleInputChange }
        onChange         = { this.trackChanges }
        defaultValue     = { this.props.defaultValue }
        components       = {{ LoadingIndicator, DropdownIndicator, Input }}
      />

aarmel-sw avatar Jul 19 '18 21:07 aarmel-sw

why doesn't it just reload if loadOptions changes? seems like that would be simple enough, just check on componentWillReceiveProps

alexfoxgill avatar Jul 24 '18 15:07 alexfoxgill

@alexfoxgill LoadOptions is also a promise or callback. It loads the values displayed for the user to select from, not what they have already selected. What's being asked here is to clear what has already been selected. So in this case load options is not changed at all.

aarmel-sw avatar Jul 24 '18 21:07 aarmel-sw

i think we have our wires crossed. i want to use the component like this:

interface Props {
  externalValue: string
  value: string
}

interface State {
  loadOptions: (search: string) => Promise<string[]>
}

class MyComponent extends React.Component<Props, State> {
  constructor(props: Props) {
    super(props)
    this.state = {
      loadOptions: (search: string) => callExternalApi(props.externalValue)
    }
  }

  componentWillReceiveProps(newProps: Props) {
    if (newProps.externalValue !== this.props.externalValue) {
      this.setState({
        loadOptions: (search: string) => callExternalApi(newProps.externalValue)
      })
    }
  }

  render() {
    return <AsyncSelect
      value={this.props.value}
      loadOptions={this.state.loadOptions} />
  }
}

do you see? loadOptions should reload whenever the function changes

alexfoxgill avatar Jul 26 '18 09:07 alexfoxgill

My bad I was thinking of something completely different. In the case here its a externalValue prop that is changing not the function, the function is then meant to receive a different value. Unless you changed the actual function name or something.

aarmel-sw avatar Jul 26 '18 20:07 aarmel-sw

when externalValue changes, the this.state.loadOptions function is recreated. this means it is a different object reference and can be compared by the component

alexfoxgill avatar Jul 27 '18 10:07 alexfoxgill

This works for me:

<Async
  key={JSON.stringify(this.state.someUpdatedProp)}
  ...
  />

Example working: https://github.com/helmarjunior/react-hooks-select-async

helmarluiz avatar Jul 28 '18 18:07 helmarluiz

@helmarluiz that works well, thanks!

alexfoxgill avatar Jul 30 '18 16:07 alexfoxgill

This does not work when a truthy isMulti property is passed.

Steague avatar Sep 27 '18 16:09 Steague

This works for me:

<Async
  key={JSON.stringify(this.state.someUpdatedProp)}
  ...
  />

how to you do this, as i want to update the options with the user input when serachable={true} but when i use key, it closes after user type a letter

rebirthtobi avatar Oct 18 '18 14:10 rebirthtobi

I have also applied the hack from @helmarluiz for now but I don't understand why the cacheOptions object doesn't already handle this as it appears to be what it is for. The documentation is a little misleading:

/* If cacheOptions is truthy, then the loaded data will be cached. The cache will remain until cacheOptions changes value. Default: false. */

If the cacheOptions is set to false this does not stop the cache being populated. (The opposite of if truthy the loaded data is cached) it only affects whether the cache is reset when the value changes.

If you provide an object and it changes it says it will clear the optionCache but it does not take into account the defaultOptions being set to true and reload them in this case.

I would like to see a native solution to this issue as it appears to be one a lot of people are suffering from... or guidance on how we should natively force the defaultOptions to reload based on external props in our own component.

The most common situation for this in our app is that we are using AsyncSelect to load all options and then filtering them based on what is in another array. When this array changes we need the options to reload and be re-filtered. Adding optionCache={this.props.items} does not appear to work as even if the existing cache is cleared the loadOptions promise is not re-called to re-populate the options so they can be re-filtered.

@JedWatson Please can you advise how this workflow is supposed to be handled natively by the latest version of your async component?

nokternol avatar Feb 01 '19 13:02 nokternol

hack from @helmarluiz works for me, thanks for that! Anyway there should be some other option to force reload default options ... definitely - this state is not sufficient.

ketysek avatar Feb 02 '19 16:02 ketysek

for anyone using isMulti like me (@Steague for example), my workaround was to use a combination of local state and componentDidUpdate to manually set defaultOptions (it also accepts array of objects, instead of just boolean), whenever a property changes.

azraelx23 avatar Feb 04 '19 17:02 azraelx23