1
case "CONNECTOR_CONNECTION_FULFILLED":
    state = {
        ...state,
        appState: 'loggedIn'
    };
    state.ourPlayer = { ...action.payload.player }

    break;

So when my app fires this action, it creates a player object for my app to use. The player object has prototype methods (player.spawn(), player.killed(), etc) on it though and when I copy it this way those don't get copied.

1
  • 2
    The Redux docs strongly recommend against putting non-serializable items (like functions) in your store. If your Redux reducers modify player state, then you should make them plain objects and use reducers instead of instance methods like killed. If your reducers don't modify the player state, then you should just do state.ourPlayer = action.payload.player, Commented Jul 7, 2017 at 16:31

1 Answer 1

1

The { ...someObject } object spread syntax, as well as the Object.assign method copies only the object's own enumerable properties. Non enumerable props, the prototype props, and the prototype itself are not copied.

So, while, that's not a recommended practice in the redux world, you can create some kind of a copy constructor for your domain objects, for example:

class Player {
  constructor(anotherPlayer) {
    // copy the props of another player there
  }
}

And use it in the reducer:

case "CONNECTOR_CONNECTION_FULFILLED":
   state = {
     ...state,
     appState: 'loggedIn',
   };
   state.ourPlayer = new Player(action.payload.player)

   break;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.