简体   繁体   English

Spread Operator不适用于基于Redux / ES6的样本

[英]Spread Operator not working for Redux/ES6 based sample

I am trying to understand Redux online tutorials that are posted by Dan Abramov. 我正在尝试理解Dan Abramov发布的Redux在线教程。 At present I am on the following sample: 目前我在以下样本:

Reducer composition with Arrays 减速机组成与阵列

Here is my practice code following the above sample: 以下是我在上面的示例后的练习代码:

// Individual TODO Reducer
const todoReducer = (state, action) => {
    switch(action.type) {
    case 'ADD_TODO':
        return {
            id: action.id,
            text: action.text,
            completed: false
          };
    case 'TOGGLE_TODO':
        if (state.id != action.id) return state;

      // This not working
      /*
      return {
        ...state,
        completed: !state.completed
      };
      */

      //This works
      var newState = {id: state.id, text: state.text, completed: !state.completed};
      return newState;
    default:
        return state;
  }
};

//TODOS Reducer
const todos = (state = [], action) => {
        switch(action.type) {
        case 'ADD_TODO':
       return [
          ...state,
          todoReducer(null, action)
       ];
       case 'TOGGLE_TODO':
        return state.map(t => todoReducer(t, action));
      default:
        return state;
    }
};

//Test 1
const testAddTodo = () => {
  const stateBefore = [];

  const action = {
      type: 'ADD_TODO',
      id: 0,
      text: 'Learn Redux'
  };

  const stateAfter = [{
     id: 0,
     text: "Learn Redux",
     completed: false
  }];

  //Freeze
  deepFreeze(stateBefore);
  deepFreeze(action);

  // Test
  expect(
     todos(stateBefore, action)
  ).toEqual(stateAfter);
};

//Test 2
const testToggleTodo = () => {
  const stateBefore = [{id: 0,
     text: "Learn Redux",
     completed: false
  }, {
    id: 1,
    text: "Go Shopping",
    completed: false
  }];

  const action = {
      type: 'TOGGLE_TODO',
      id: 1
  };

  const stateAfter = [{
     id: 0,
     text: "Learn Redux",
     completed: false
  }, {
    id: 1,
    text: "Go Shopping",
    completed: true
  }];

  //Freeze
  deepFreeze(stateBefore);
  deepFreeze(action);

  // Expect
  expect(
     todos(stateBefore, action)
  ).toEqual(stateAfter);
};

testAddTodo();
testToggleTodo();
console.log("All tests passed");

Issue is, within the todoReducer function, following syntax is not working: 问题是,在todoReducer函数中,以下语法不起作用:

return {
        ...state,
        completed: !state.completed
      };

I am using Firefox version 44.0 and it shows me following error in console: 我使用的是Firefox 44.0版,它在控制台中显示以下错误:

Invalid property id

Now I guess my current version of Firefox must support Spread operator. 现在我猜我当前版本的Firefox必须支持Spread运营商。 If anyway it does not, is there any way to add some standalone Polyfill to support this syntax? 如果它不是,有没有办法添加一些独立的Polyfill来支持这种语法?

Here is also the JSFiddle 这里也是JSFiddle

The object spread syntax is not supported in most browsers at the minute . 大多数浏览器不支持对象扩展语法 It's proposed for addition in ES7 (aka ES2016). 它建议在ES7(又名ES2016)中添加。 As far as I know there's no way to polyfill it, as it uses a new syntax rather than just being a function call. 据我所知,没有办法对它进行填充,因为它使用的是新语法,而不仅仅是函数调用。

You have two options in the meantime. 在此期间您有两种选择。

1) Use Object.assign to create an updated version of the object, like so: 1)使用Object.assign创建对象的更新版本,如下所示:

Object.assign({}, state, {
  completed: !state.completed
});

Although this will also need to be polyfilled in most browsers - a good example one is available on MDN , or you can use a third party library's version, like the one in lodash . 虽然这也需要在大多数浏览器中进行填充 - 在MDN上可以使用一个很好的示例 ,或者您可以使用第三方库的版本, 例如lodash中的版本。

2) Use transpiling tools like Babel , which allow you to write your code with newer syntax and then convert it to a version that works in all browsers. 2)使用像Babel这样的转换工具,它允许您使用较新的语法编写代码,然后将其转换为适用于所有浏览器的版本。

If anyone using Babel is still having trouble, this feature may not be available with Babel out of the box, you may need to add a plugin: http://babeljs.io/docs/plugins/transform-object-rest-spread/ 如果任何使用Babel的人仍然遇到问题,开箱即用Babel可能无法使用此功能,您可能需要添加插件: http//babeljs.io/docs/plugins/transform-object-rest-spread/

then update .babelrc with 然后用。更新.babelrc

"plugins": ["transform-object-rest-spread"] “插件”:[“transform-object-rest-spread”]

You can not polyfill syntax. 你不能polyfill语法。 You need to use something like babel to compile to an older version of JavaScript if you wish to execute in current browsers. 如果您希望在当前浏览器中执行,则需要使用像babel这样的东西来编译为旧版本的JavaScript。

https://babeljs.io/ https://babeljs.io/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM