在Redux中传播属性

Chr*_*eek 0 javascript ecmascript-6 reactjs redux

我试图在我的Reducer中使用spread属性,但它回来时语法错误无效.我的构建支持使用spread运算符,因为我只在reducers中得到错误.

auth_types.js

export const AUTH_USER = 'AUTH_USER'
export const UNAUTH_USER = 'UNAUTH_USER'
Run Code Online (Sandbox Code Playgroud)

auth_actions.js

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



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

            browserHistory.push('/feature')
          })
      }
    }
Run Code Online (Sandbox Code Playgroud)

reducer.js

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

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

  return state
}
Run Code Online (Sandbox Code Playgroud)

And*_*ndy 5

文档:

由于对象扩展语法仍然是ECMAScript的第2阶段提议,因此您需要使用像Babel这样的转换器在生产中使用它.您可以使用现有的es2015预设,安装babel-plugin-transform-object-rest-spread并将其单独添加到您的插件阵列中.babelrc.

{
  "presets": ["es2015"],
  "plugins": ["transform-object-rest-spread"]
}
Run Code Online (Sandbox Code Playgroud)

请注意,这仍然是一个实验性语言功能提案,因此将来可能会发生变化.