Redux 中的传播属性 [英] Spread properties in Redux

查看:19
本文介绍了Redux 中的传播属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的减速器中使用传播属性,但它返回一个无效的语法错误.我的构建支持使用扩展运算符,因为我只在减速器中出现错误.

auth_types.js

export const AUTH_USER = 'AUTH_USER'导出 const UNAUTH_USER = 'UNAUTH_USER'

auth_actions.js

import { AUTH_USER, UNAUTH_USER } from './auth_types'导出函数 signinUser({ email, password }) {返回函数(调度){axios.post(`${ROOT_URL}/signin`, { email, password }).then(响应 => {调度({ 类型:AUTH_USER })browserHistory.push('/功能')})}}

reducer.js

import { AUTH_USER, UNAUTH_USER } from '../actions/auth_types'导出默认函数(状态 = {},动作){开关(动作.类型){案例 AUTH_USER:返回 { ...状态,已验证:真}案例 UNAUTH_USER:返回 { ...状态,已验证:假}}返回状态}

解决方案

来自文档:

<块引用>

由于对象扩展语法仍然是 ECMAScript 的第 2 阶段提案,因此您需要使用转译器(例如 Babel)才能在生产中使用它.您可以使用现有的 es2015 预设,安装 babel-plugin-transform-object-rest-spread 并将其单独添加到 .babelrc 中的 plugins 数组中.

<代码>{预设":[es2015"],插件":[转换对象休息传播"]}

<块引用>

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

I am trying to use the spread properties 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.

auth_types.js

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

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')
          })
      }
    }

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
}

解决方案

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.

这篇关于Redux 中的传播属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆