Coming from react-redux
/ redux-thunk
, there is a nice way of instantiating a client object once, passing it to ThunkMiddleware, and retrieving it in thunks:
// This is the instance I want during async calls
const myApiClient = new MyApiClient();
const store = createStore(
reducer,
applyMiddleware(thunk.withExtraArgument(myApiClient))
);
Then in my thunk definitions:
const fetchSomething = (id) => {
return (dispatch, getState, myApiClient) => {
// It's the same instance!
return myApiClient.fetchSomething(id)
.then((res) ....);
}
}
In Recoil I can see no way of achieving something similar: as far as I can see, examples in the documentation assume that the body of atoms / selectors can be executed without any context instantiated externally.
Since it seems unlikely that this was not a consideration when designing Recoil, I'm curious what I've missed here?