gpt4 book ai didi

javascript - 如何在嵌套的 Immutable.js 映射和列表中执行推送操作。?

转载 作者:行者123 更新时间:2023-11-30 15:36:03 26 4
gpt4 key购买 nike

我将 pepperoni-app-kit 用于 Immutable.js 附带的 react-native。我以前从未使用过 Immutable.js,我发现执行一个简单的任务真的很难

我想在消息列表中推送一条消息,它位于 Map 中。

someReducer.js

import {Map,List} from 'immutable';

const initialState = Map({isReady : false , messages : List([])});

// reducer switch case
return state.update('messages',messages=>messages.concat(action.Message.payloadString);
// not working
//I also tried
return state.get('messages').push(Immutable.Map({text:'some text',...}))

如何使用 Immutable.js 编写这段 ES6 代码

var newState = state; // copy state 
newState.messages.push({text:'hello world',...otherInfo}); // make changes
return object.assign({},newState); // return a new object.

最佳答案

describe('how-to-perform-push-operation-in-a-nested-immutable-js-map-and-lists', () => {
const initialState = Immutable.Map({
isReady: false,
messages: Immutable.List([]),
});

it('should `push` using List.update', () => {
const payload = 'some text';
const updatedState = initialState.update('messages',
(list) => (list.push(payload))
);
// passes:
expect(updatedState.getIn(['messages', 0])).to.equal(payload);
});

it('should `concat` using List.update', () => {
const multiMessagePayload = ['alpha', 'beta'];
const reUpdatedState = initialState.update('messages',
(list) => (list.concat(multiMessagePayload))
);
// passes:
expect(reUpdatedState.get('messages').size).to.equal(2);
});
});

关于javascript - 如何在嵌套的 Immutable.js 映射和列表中执行推送操作。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41530798/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com