最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - redux - how to store and update a keyvalue pair - Stack Overflow

programmeradmin6浏览0评论

I am using redux wth reactjs.

I want to store simple key/value pairs but can't get the reducer syntax right.

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.

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

    default:
      return state
  }
}

I am using redux wth reactjs.

I want to store simple key/value pairs but can't get the reducer syntax right.

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.

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

    default:
      return state
  }
}
Share Improve this question edited Nov 14, 2017 at 19:46 Petr Shypila 1,5099 gold badges27 silver badges47 bronze badges asked Feb 15, 2016 at 7:52 Duke DougalDuke Dougal 26.4k33 gold badges96 silver badges133 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 12

This worked for me:

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

From the docs:

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

You just have a couple mistakes with {} instead of [] and forgetting to use Object.assign.

const reducer = (state = {}, action) => {
  switch (action.type) {
    case 'addConnection':
      return Object.assign({}, state, {
        connections: [
           ...state.connections,
           {
             [actions.positeKey]: 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, positeKey, connection}) => {
  switch (type) {
    case 'addConnection':
      return Object.assign({}, state, {
        connections: state.connections.concat({
          [positeKey]: connection
        })
      });
    default:
      return state;
  }
}

export default reducer;

Or if you're using Immutable, something like this

import Immutable from 'immutable';

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

export default reducer;

This may work

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

export default reducer;
发布评论

评论列表(0)

  1. 暂无评论