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.

What about setState?

See original GitHub issue

Recently effects and reducers got merged into actions. Now the way to set the state is by just retuning an object.

What about exposing a actions.setState function which can be called from everywhere where actions are available instead? This way an effect could still return whatever it wants when called by a different action (I actually used that a lot) and code like this becomes easier to do:

function example () {
	actions.room.set(room) // reducer
	actions.room.socket.join() // effect
	// the key here is that socket.join depends on model.room
}
function example () {
	actions.setState({room})
	actions.room.socket.join()
}

This would also fix use-cases where big actions need to be split into multiple actions, but need to return data that is not a promise. Hard to describe, but I actually also used that for WebRTC SDP patching. I worked around this, but overall I think that this might make hyperapp easier to use for more advanced apps.

And yes, I’m aware that setState() would basically be implemented as setState: (_, data) => data.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:51 (30 by maintainers)

github_iconTop GitHub Comments

1reaction
jorgebucarancommented, Mar 15, 2017

Sorry, I don’t understand. There is only one way to update the model.

actions: {
   updateTheModel: _ => newModel
}
1reaction
jorgebucarancommented, Mar 15, 2017

Yeah, now I finally understand what we are talking about.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReactJS setState() - GeeksforGeeks
setState is asynchronous call means if synchronous call get called it may not get updated at right time like to know current value...
Read more >
Everything you need to know about setState() - Medium
To better understand what setState() does, let's first take a look at what the state of a React component is. The state allows...
Read more >
ReactJS State: SetState, Props and State Explained [Updated]
The setState() Method. State can be updated in response to event handlers, server responses, or prop changes. This is done using the setState() ......
Read more >
React.Component
setState() enqueues changes to the component state and tells React that this component and its children need to be re-rendered with the updated...
Read more >
Understanding React `setState` | CSS-Tricks
When the request to setState() is triggered, React creates a new tree containing the reactive elements in the component (along with the updated ......
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