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.

Hi, I tried to use an object in my state but it seems like there is an issue, I have to dispatch three times to get the updated state in my component.

This is the code:

const initialState = {
  game: { pin: null }
};

export const reducer = (state = initialState, action) => {
  console.log('state', state);
  switch (action.type) {
    case 'setNewGame':
      return {
        ...state,
        game: {
          ...state.game,
          pin: action.gamePin
        }
      };
    default:
      return state;
  }
};

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dvlpr-ethcommented, Mar 12, 2019

Thanks a lot for thunk example!

1reaction
dai-shicommented, Mar 12, 2019

Just added 09_thunk. Hope it helps.

Let me close this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Updating Objects in State
State can hold any kind of JavaScript value, including objects. But you shouldn't change objects that you hold in the React state directly....
Read more >
Updating an object with setState in React
To update the status of any todo object, run a map on the array and check for some unique value of each object,...
Read more >
React State
The state object is where you store property values that belongs to the component. When the state object changes, the component re-renders. Creating...
Read more >
State In React: An overview
State is a plain JavaScript object used by React to represent an information about the component's current situation.
Read more >
Using React useState with an object
Learn how to use useState when working with objects by creating a temporary object and using object destructuring to create a new object....
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