Comments (7)
That's actually the problem. A selector is, by definition, 100% synchronous and pure (same as a reducer, actually). It gets called synchronously, is expected to return a value immediately, and that value is based solely on the arguments that were passed in (typically the Redux store state
, plus optional parameters).
You can write other async function that are similar (in that they take the state as an argument), but they would no longer be selectors.
Where and how would you expect to use one of these "async selectors"?
from reselect.
Appreciate very quick response 🙏
We use https://jsonata.org. It's very simple query language that "unfortunately" works async (in a future version). Here is a simplified example:
const asyncSelector = createAsyncSelector(
(state) => state.a,
(state) => state.b,
async (a, b) => {
const context = { a, b }
return await jsonataEvaluator("a+b", context)
}
Then in React world I would do this:
function PrintAsyncSelector() {
const result = useSelector(asyncSelector)
return <p>{result}</p>
}
function Component() {
return (
<Suspense>
<PrintAsyncSelector />
</Suspense>
)
}
The Component
is suspended while evaluating asyncSelector
. That would mean the useSelector
would need to throw promise.
Another use case would be in "side effects". If you want to get current and derivated data from the store in callback executed on click.
function Button() {
const onSubmit = async () => {
const result = await asyncSelector(store.getState())
await doRestCallWithCurrentValue(result)
}
return <Button onClick={onSubmit}>Submit</Button>
}
I know I could do the await jsonataEvaluator("a+b", context)
directly in the onSubmit
callback, but I'd like to re-use the same code from the selector.
Hope it makes sense.
from reselect.
Yeah, unfortunately that won't work with Redux. useSelector
(and the underlying React useSyncExternalStore
hook) do expect standard 100% synchronous selector functions.
What are you actually trying to retrieve with JSONata? is it actual data in the Redux store state?
from reselect.
Yes, it is data in the Redux store. It is very complex network of createSelector
that collects data from redux and then use jsonata to evaluate the expression.
I guess I could do something like this:
function useHook() {
const [result, setResult] = useState()
const context = useSelector(collectDataFromRedux)
useEffect(() => {
setResult(await jsonataEvaluator("a+b", context))
}, [context])
return result
}
But that complicates the code and it feels like unnecessary work-around of React's Suspense.
from reselect.
you could try using the suspense util library for it?
from reselect.
Part of the issue is it seems like the entire state
is needed as an argument. Normally to get that you'd have to use useSelector(state => state)
, which would force a re-render 100% of the time and is bad.
from reselect.
Sorry, maybe it is just a lack of knowledge about all these libraries. But if I am not wrong there is no problem of making createSelector
work async, right? We can discuss what exactly pure function means and how strict we need to be about it but for the sake of conversation let's say that the following would still be pure action.
We use await
/async
and the jsonata (in our case) evaluates the same inputs to the same outputs without any side effects, I think we can consider such a function as pure.
The problem is the connection between React and Redux, where useSelector
doesn't wait for the reselect's selector to be done. But that shouldn't be a problem to support with a few adjustments, right?
I put together this code sandbox that might better illustrate what I mean - https://codesandbox.io/p/sandbox/use-external-storage-forked-yflsy3
Please let me know if I am missing something
from reselect.
Related Issues (20)
- unstable_autotrackMemoize Errors HOT 1
- How to type redux state and selectors with readonly? HOT 3
- Consider dev mode checks for `x => x` result functions HOT 2
- More Reselect addons to investigate HOT 1
- Add identifiable information to dev mode check log messages HOT 3
- Documentation ignores links in the table of contents entries HOT 1
- Type loss in `createSelector` with inline function declarations passed as separate arguments
- lastResult.deref is not a function (it is undefined) HOT 6
- Better call stack for selector warnings HOT 11
- Unable to use `resultEqualityCheck` with `weakMapMemoize` HOT 3
- Incorrect weakMapMemoize alternative example using useCallback HOT 1
- using createSelector.withTypes prevents build HOT 7
- TypeError: (0 , _reselect.createSelector) is not a function HOT 12
- `weakMapMemoize` with `resultEqualityCheck` is provided empty objects for first call. HOT 3
- Library do not work in Safari < 14.1 HOT 1
- Why the LRUCache implementation is using Array over the Doubly Linked List with Map? HOT 8
- Question: should OutputSelector be used as an InputSelector? HOT 3
- Current documentration loose article about passing parameters HOT 1
- First level of cascading memoization is broken when more props are passed HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reselect.