Storing and instance instead of an array in rxjs5 Store

53 views Asked by At

I have the following attempt at a reducer

import { ActionReducer, Action, Store } from '@ngrx/store';
import { Observable } from 'rxjs'
import { Name } from './name.model'

export const ADD_NAME = 'ADD_NAME';

export const name: ActionReducer<any> = ( state = new Name(), action: Action ) => {
  switch ( action.type ) {
    case ADD_NAME:
      return [ state, action.payload ];

    default:
      return state;
  }
};

I would like to store a Name instance in the Store. Currently the ADD_NAME returns an array. How can I store just a single instance instead of array

1

There are 1 answers

1
Sasxa On BEST ANSWER

You can write this with typescript 2.1+:

case ADD_NAME:
  return { ...state, action.payload };

or

case ADD_NAME:
  return Object.assign({}, state, action.payload);