简体   繁体   English

redux - 如何存储和更新键/值对

[英]redux - how to store and update a key/value pair

I am using redux wth reactjs. 我正在使用redux wth reactjs。

I want to store simple key/value pairs but can't get the reducer syntax right. 我想存储简单的键/值对,但无法正确获得reducer语法。

In this case each key/value pair will hold a connection to an external system. 在这种情况下,每个键/值对将保持与外部系统的连接。

Is this the right way to do it? 这是正确的方法吗? I'm at the beginning with redux so it's a bit of mystery. 我正在开始使用redux,所以它有点神秘。

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

    default:
      return state
  }
}

You just have a couple mistakes with {} instead of [] and forgetting to use Object.assign . 你只有{}而不是[]的一些错误而忘记使用Object.assign

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;

It might help to see it expressed this way too. 看来它也表达了这种方式可能会有所帮助。 It does the same thing but I think it reads a little nicer 它做同样的事情,但我认为它看起来更好一点

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;

Or if you're using Immutable , something like this 或者,如果你使用的是Immutable ,就像这样

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;

This worked for me: 这对我有用:

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

From the docs: 来自文档:

https://redux.js.org/recipes/structuring-reducers/immutable-update-patterns#correct-approach-copying-all-levels-of-nested-data https://redux.js.org/recipes/structuring-reducers/immutable-update-patterns#correct-approach-copying-all-levels-of-nested-data

This may work 这可能有效

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;

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

相关问题 如何更新 id - 键值对的反应 redux state Object - How to update react redux state Object of id - key value pair 如何在键为数字的数组中存储键值对 - How to store key value pair in array where key is a number 如何使用动态键值对将数据存储在 Chrome 存储 API 中? - How to store data in Chrome Storage API with dynamic key value pair? 如何从字符串中提取子字符串并将它们存储为键值对 - How to extract substrings from string and store them as key value pair 如何更新 Javascript 中嵌套对象数组中的键/值对 - How to update a key/value pair in a nested array of objects in Javascript 如何更新计时器上的 Redux 存储值? - How do I update a Redux store value on a timer? Redux Reducer-根据ID过滤数组,然后更改键/值对 - Redux Reducer - Filter array based on id then change key/value pair 如何将键值对转换为新的键值对javascript - How to turn key value pair to new key value pair javascript 如何将100个不同的选择名称及其选定的选项存储在对象中作为键值对 - how do you store 100s of different select names and their selected options in a object as a key value pair 如何赋予$('inputs')每个函数以将数据存储为asp.net中的键值对 - how to give $('inputs') each function as to store data as key value pair in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM