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.

How does actions change the state?

See original GitHub issue

I’m wondering How does actions change the state? Is it similar to redux? I’m looking at the counter example which is pretty simple but is it immutable?

actions: {
    down: state => ({ count: state.count - 1 }),
    up: state => ({ count: state.count + 1 })
  }

##Lets say an example like this

var state = {
  property: {
    propertyChild: [{
        city: 'New York'
      },
      {
        city: 'Santiago'
      },
      {
        city: 'Tokyo'
      }
    ]
  }
}

what if I wanted to change the second object in propertyChild?

what would be the proper way to update that?

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
jorgebucarancommented, Aug 30, 2017

@codingphasedotcom

I’m wondering How does actions change the state?

We wrap each action you pass to the app call in a function like this:

wrappedAction = function(someData) {
  var result = yourAction(state, actions, someData)
  return updateTheStateWith(result)
}

This way we can update the state every time an action returns. This in turn causes the UI to re-render. Note that in detail, this is more efficient than just re-rendering every single time. We only re-render the UI on the next browser repaint.

Is it similar to redux?

Yes, quite so.

I’m looking at the counter example which is pretty simple but is it immutable?

Yes, it is. The way we update the current state is by replacing it with a new state, never mutating the previous one.

1reaction
andyrjcommented, Aug 30, 2017

@codingphasedotcom I would recommend sticking with plain old JavaScript until you have to get into really nested state, at which point qim is a pretty nice library if you aren’t happy with immutability-helper. The default for return of an action is a shallow merge like @selfup said. It would be like redux only having 1 reducer that just does, state = Object.assign({}, state, actionResult). You could also change that with a custom resolve if you want different behavior.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Actions and reducers: updating state - Human Redux
In Redux all changes to the application state happen via an "action." An "action" in Redux terms is simply "a thing that happened"...
Read more >
Redux Fundamentals, Part 3: State, Actions, and Reducers
The official Redux Fundamentals tutorial: learn how reducers update state in response to actions.
Read more >
Why is it beneficial to change state w/ actions in redux?
State is read-only. The only way to change the state is to emit an action, an object describing what happened. This ensures that...
Read more >
Redux: Describing State Changes with Actions - Egghead.io
The only way to change the state tree is by dispatching an action. An action is a plain JavaScript object, describing in the...
Read more >
Changing the System State Using a Custom Action - Win32 apps
Custom actions that set properties, feature states, component states, or target directories, or schedule system operations by inserting rows ...
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