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.

Proposal:

function useSelector(selector) {
  const store  = React.useContext(StoreContext);
  const [state, setState] = React.useState(selector(store.get()));

  React.useEffect(() => {
    setState(selector(store.get()));
  }, [selector]);

  store.on('@changed', () => {
    setState(selector(store.get()));
  })

  return state;
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
aicommented, Jul 11, 2019

Sorry, but these bindings are important for performance. I do not want to add bad for performance solution.

You can write own custom hook, it is very easy.

0reactions
pret-a-portercommented, Jul 11, 2019

I tried it, it is not so convenient.

  1. With big store I need to write each time construction like useStoreon('one', 'two', ... 'ten'); For example, twenty hooks with this construction…
  2. When I will change structure of my store, I will need to change every hook and every selector function. With useSelector hook I will need to change only selector functions.
  3. I use Typescript, with useSelector hook no need to describe each selector function params. Selectors always based on whole state.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Hooks - React Redux
When an action is dispatched, useSelector() will do a reference comparison of the previous selector result value and the current result value.
Read more >
UseSelector and UseDispatch: A Guide to React-Redux Hooks
useSelector and useDispatch are a set of hooks to use as alternatives to the existing connect() higher-order component. The equivalent of map ...
Read more >
React-Redux Hooks: useSelector and useDispatch
These Hooks allow you to connect to the Redux store and dispatch actions without having to wrap your components in connect() . This...
Read more >
Introduction React-Redux using Hooks (useSelector ...
We will invoke useDispatch and store it to a variable, dispatch . This hook returns a reference to the dispatch function from the...
Read more >
React Redux Tutorials - 20 - useSelector Hook - YouTube
React Redux Tutorials - 20 - useSelector Hook. Watch later. Share. Copy link. Info. Shopping. Tap to unmute.
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