Chris Meek Chris Meek - 2 years ago 164
Javascript Question

Spread Operators in Redux

I am trying to use the spread operator in my reducers, but it is coming back with an invalid syntax error. My build supports the use of the spread operator as I only get the error in my reducers.


export const AUTH_USER = 'AUTH_USER'
export const UNAUTH_USER = 'UNAUTH_USER'


import { AUTH_USER, UNAUTH_USER } from './auth_types'

export function signinUser({ email, password }) {
return function(dispatch) {`${ROOT_URL}/signin`, { email, password })
.then(response => {
dispatch({ type: AUTH_USER })



import { AUTH_USER, UNAUTH_USER } from '../actions/auth_types'

export default function(state = {}, action) {
switch(action.type) {
return { ...state, authenticated: true }
return { ...state, authenticated: false }

return state

Answer Source

From the documentation:

Since the object spread syntax is still a Stage 2 proposal for ECMAScript you’ll need to use a transpiler such as Babel to use it in production. You can use your existing es2015 preset, install babel-plugin-transform-object-rest-spread and add it individually to the plugins array in your .babelrc.

  "presets": ["es2015"],
  "plugins": ["transform-object-rest-spread"]

Note that this is still an experimental language feature proposal so it may change in the future.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download