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.

Equivalent to react's setState callback?

See original GitHub issue

I have a need to execute arbitrary logic only after a state change has been applied. In react land, I can accomplish this by running setState(state, cb). Based on my extremely limited hyperapp experience, this appears to be impossible since, in order to update state, an action must return.

Is there already a means of doing what I want in hyperapp? If not, is there any interest in adding this feature?

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
zacenocommented, Mar 19, 2018

@jpodwys FWIW: I sometimes make a generic reduce action, to use, for example as a callback in promises.

actions = {
  reduce: fn => s => fn(s),
  myAction: x => (state, actions) => {
    doSomethingAsync(state.foo).then(result => actions.reduce(state => {
      //now I have the current state after the update 
    }))
  }
}

… it’s along the lines of your setState action, but without the actual state-setting part 😉 It suffers from the same problem though: you have to repeat it in every namespace where you’ll need it, if you’re using nested actions. But since it’s not a very common need, and the action is so small, it’s no big deal.

0reactions
jpodwyscommented, Mar 19, 2018

It’s not a bad idea, but you will lose access to it once you start using nested actions.

Oh right, I’m not using nested state/actions so I hadn’t considered this. Good catch. Your solution makes sense, I’ll give it a go!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use `setState` callback on react hooks - Stack Overflow
setState method is asynchronous, and as a matter of fact, it does not return a promise. So In cases where we want to...
Read more >
How to Use the setState Callback in React - Upmostly
To perform an action in a React component after calling setState, such as making an AJAX request or throwing an error, we use...
Read more >
How to Use callBack With setState in React - CODERSERA
setState () enqueues changes to the component state and tells React that this component and its children need to be re-rendered with the...
Read more >
React setState calback with Functional Component - Medium
useState returns the current state and a function to update it. But this function updates the value in an asynchronous way.
Read more >
Provide callback to useState hook like setState - LinkedIn
In React functional components, a callback function for anything can be implemented using the useEffect hook. We will be using the same to ......
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