I try to implement a async react-select (Select.Async). The problem is, we want to do the fetch in redux-saga. So if a user types something, the fetch-action should be triggered. Saga then fetches the record and saved them to the store. This works so far. Unfortunately loadOptions has to return a promise or the callback should be called. Since the newly retrieved options get propagated with a changing property, I see no way to use Select.Async together with saga to do the async fetch call. Any suggestions?
<Select.Async
multi={false}
value={this.props.value}
onChange={this.onChange}
loadOptions={(searchTerm) => this.props.options.load(searchTerm)}
/>
I had a hack where i assigned the callback to a class variable and resolve it on componentWillReceiveProps. That way ugly and did not work properly so i look for a better solution.
Thanks
redux-saga
is for handling side effects like asynchronously receiving options forreact-select
. That's why you should leave the async stuff toredux-saga
. I have never usedreact-select
but by just looking at the documentation I would solve it this way:Your component gets very simple. Just get
value
andoptions
from your redux store.optionsRequested
is an action creator for theOPTIONS_REQUESTED
action:A saga definition watches for
OPTIONS_REQUESTED
action that is trigged byonInputChange
, loads the data with givensearchTerm
from server and dispatchesOPTIONS_RECEIVED
action to update redux store.In other words: Make your Component as pure as possible and handle all side-effect/async calls in
redux-saga
I hope this answer was useful for you.