Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript redux-如何存储和更新密钥/值对_Javascript_Reactjs_Redux - Fatal编程技术网

Javascript redux-如何存储和更新密钥/值对

Javascript redux-如何存储和更新密钥/值对,javascript,reactjs,redux,Javascript,Reactjs,Redux,我正在使用reactjs的redux 我想存储简单的键/值对,但无法获得正确的reducer语法 在这种情况下,每个键/值对将保持与外部系统的连接 这样做对吗?我刚开始使用redux,所以有点神秘 export default (state = {}, action) => { switch(action.type) { case 'addConnection': return { connections: { ...sta

我正在使用reactjs的redux

我想存储简单的键/值对,但无法获得正确的reducer语法

在这种情况下,每个键/值对将保持与外部系统的连接

这样做对吗?我刚开始使用redux,所以有点神秘

export default (state = {}, action) => {
  switch(action.type) {
    case 'addConnection':
      return    {
        connections: {
          ...state.connections, {
          action.compositeKey: action.connection
        }
      }

    default:
      return state
  }
}

您只是在使用
{}
而不是
[]
时犯了几个错误,并且忘记使用
对象。分配

const reducer = (state = {}, action) => {
  switch (action.type) {
    case 'addConnection':
      return Object.assign({}, state, {
        connections: [
           ...state.connections,
           {
             [actions.compositeKey]: action.connection
           }
        ]
      });
    default:
      return state;
  }
}

export default reducer;
这样表达可能也会有所帮助。它做同样的事情,但我认为它读起来更好一点

const reducer = (state = {}, {type, compositeKey, connection}) => {
  switch (type) {
    case 'addConnection':
      return Object.assign({}, state, {
        connections: state.connections.concat({
          [compositeKey]: connection
        })
      });
    default:
      return state;
  }
}

export default reducer;
或者如果你正在使用,像这样的东西

import Immutable from 'immutable';

const reducer = (state = Immutable.Map(), {type, compositeKey, connection}) => {
  switch (type) {
    case 'addConnection':
      return state.set(
        'connections',
        state.get('connections').concat({
          [compositeKey]: connection
        })
      );
    default:
      return state;
  }
}

export default reducer;
这可能有用

const reducer = (state = {}, {type, compositeKey, connection}) => {
  switch (type) {
    case 'addConnection':
      var newData={};
      newData[compositeKey]=connection;
      return Object.assign({}, state, newData)
      });
    default:
      return state;
  }
}

export default reducer;
这对我很有用:

export default (state = {}, action) => {
  switch(action.type) {
    case 'addConnection':
      return {
        ...state,
        connections: {
          ...state.connections,
          [action.compositeKey]: action.connection
        }
      }
    default:
      return state
  }
}
从文档中:


当我使用您提供的第二个或第三个选项时,它在编译时显示“未捕获引用错误:未定义操作”。我忘记在
操作的分解中指定
类型。他们现在应该很好:)