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.

Should state be kept separate from callbacks in the returned result?

See original GitHub issue

Currently 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:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Noitidartcommented, Mar 27, 2019

Thank you! Super awesome!

0reactions
pelotomcommented, Mar 27, 2019

@Noitidart yes, except that I’ve also reversed the argument order (#3) to be consistent with that of useReducer, so:

const [count, { increment, decrement }] = useMethods(methods, initial);
Read more comments on GitHub >

github_iconTop 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 >

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