question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return multiple actions from useStoreActions?

See original GitHub issue

So while the naming of useStoreActions would imply you can return multiple actions from a single call, is that safe/performant to do?

For example:

  const { attachPaymentMethod, submit } = useStoreActions(x => ({
    attachPaymentMethod: x.attachPaymentMethod,
    submit: x.submit
  }))

I ask because it seems to be recommended that you return only a single value from useStoreState, so wandered if the same applies?

thx

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:25 (24 by maintainers)

github_iconTop GitHub Comments

5reactions
joelmosscommented, Jul 25, 2019

That is one way, but I think that being able to pass a custom equality function to useStoreState is far more flexible…

const equalityFunction = (newState, oldState) => {
  // Some equality check here to compare newState to oldState.
  // For example, we could use lodash's `isEqual`...
  return _.isEqual(newState, oldState)
}

useStoreState(state => ({ foo: state.foo, bar: state.bar }, equalityFunction)

If the equality function is not given, it uses the current strict equality check - maintaining the current functionility.

This obviously allows the user to perform any kind of comparison that they want without dictating too much.

3reactions
allprocommented, Jul 27, 2019
useStoreState(state => ({ foo: state.foo, baz: !state.bar }), equalityFunction);

If you add the ability to pass an equality function like this, it would be nice to also accept an options-hash. I think most uses of this (possibly 99.99%!) would be shallow-equal. Rather than require a separate utility import for this basic need, an ‘equality type’ could be accepted, something like:

useStoreState(state => ({ foo: state.foo, baz: !state.bar }), { shallowEqual: true });

A shallow-equal method is just a couple lines of code so would not bloat the library.

Shallow-equal is the standard comparison offered by React (props/state) and React-Redux (mapStateToProps), so this would replicate that. Anything more complex can be handled by a custom equalityFunction.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dispatching Multiple Actions from NGRX Effects - Medium
In this example, I need to do 2 actions in a single effect but @Effect expects us to return a new action. How...
Read more >
How to use the easy-peasy.useStoreActions function in ... - Snyk
To help you get started, we've selected a few easy-peasy. ... state.progress.continuous); const setProgress = useStoreActions(actions => actions.progress.
Read more >
Primary API - Easy Peasy v5
Don't execute any side effects within your action. Creating a Store ... The useStoreActions hook ... You can return data out of a...
Read more >
Actions | Pinia
It is possible to observe actions and their outcome with store.$onAction() . The callback passed to it is executed before the action itself....
Read more >
Easy-Peasy State Management Using React Easy Peasy
removeNote); const toggleNote = useStoreActions(actions => actions.toggleNote); return ( <li className= ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found