Coder Social home page Coder Social logo

Comments (7)

markerikson avatar markerikson commented on May 27, 2024

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.

xotahal avatar xotahal commented on May 27, 2024

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.

markerikson avatar markerikson commented on May 27, 2024

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.

xotahal avatar xotahal commented on May 27, 2024

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.

EskiMojo14 avatar EskiMojo14 commented on May 27, 2024

you could try using the suspense util library for it?

from reselect.

markerikson avatar markerikson commented on May 27, 2024

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.

xotahal avatar xotahal commented on May 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.