Should state be kept separate from callbacks in the returned result?
See original GitHub issueCurrently we return an object from useMethods
which is a mixture of state and callbacks:
const initial = { count: 0 };
const methods = state => ({
increment() {
state.count++;
},
decrement() {
state.count--;
},
});
// ...
const { count, increment, decrement } = useMethods(initial, methods);
but maybe they should be kept separate, a la useState
and useReducer
:
const [{ count }, { increment, decrement }] = useMethods(initial, methods);
One advantage of this would be the option to use primitives instead of objects for state. For example in this case we could use a simple number for the state:
const initial = 0;
const methods = count => ({
increment: () => count + 1,
decrement: () => count - 1,
});
// ...
const [count, { increment, decrement }] = useMethods(initial, methods);
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
React hooks: accessing up-to-date state from within a callback
In React hooks, due to the way state is encapsulated in the functions of React.useState() , if a callback gets the state through...
Read more >How State Works in React – Explained with Code Examples
State is the most complex thing in React, and it's something both beginners and experienced developers struggle to understand.
Read more >Hooks FAQ - React
Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class. This page...
Read more >Understanding the Event Loop, Callbacks, Promises, and ...
A promise can have three possible states: pending, fulfilled, and rejected. Pending - Initial state before being resolved or rejected; Fulfilled ...
Read more >Continued support for async tests with done callbacks #1893
You say that some people expect the test to finish when the returned Promise completes even if done was not called, but that...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thank you! Super awesome!
@Noitidart yes, except that I’ve also reversed the argument order (#3) to be consistent with that of
useReducer
, so: