简体   繁体   English

Redux:使用 Redux 更新阵列的 State

[英]Redux: Updating State of an array using Redux

Here is the question:这是问题:

In manageFriends.js, write a function called manageFriends that takes in the previous state and an action as its argument.在 manageFriends.js 中,编写一个名为 manageFriends 的 function,它接受前面的 state 和一个动作作为其参数。 Here, the initial state should be an object with a key, friends, set to an empty array.在这里,最初的 state 应该是一个 object 键,朋友们,设置为一个空数组。 This time, the reducer should be able to handle two actions, "friends/add" and "friends/remove".这一次,reducer 应该能够处理两个动作,“friends/add”和“friends/remove”。 When adding a friend, the action will include a friend key assigned to an object with name, hometown, and id keys.添加朋友时,该操作将包括分配给 object 的朋友键,其中包含姓名、家乡和 id 键。

 action = {
   type: "friends/add",
   payload: {
     name: "Chrome Boi"
     homewtown: "NYC",
     id: 1
   }
 } 

When our reducer receives "friends/add", it should return a new state with this friend object added to the friends array.当我们的 reducer 收到“friends/add”时,它应该返回一个新的 state,并将这个朋友 object 添加到朋友数组中。

Here is my code, which I am running in my js browser console这是我的代码,我在我的 js 浏览器控制台中运行

function manageFriends(state = {friends: []},
    action = {
        type: "friends/add",
        payload: {
            name: 'Mac Miller',
            hometown: 'Arizona',
            id: 1
        },
        payload: {
            name: 'Kirk Franklin',
            hometown: 'ATL',
            id: 1
        },
        type: "friends/remove",
        payload: 1,
    }){
    switch (action.type) {
        case 'friends/add':
            return {friends: [...state.freinds, action.payload]}
    case 'friends/remove':
            return {
                friends: state.friends.filter((friend) => friend.id !=action.payload)
            };
        default:
            return state;
    }
}

manageFriends(state, action)

when I run the reducer I still get an empty array.当我运行减速器时,我仍然得到一个空数组。

{friends: Array(0)}
friends: Array(0)
length: 0
[[Prototype]]: Array(0)
[[Prototype]]: Object

I founds this example that helped with the switch case statement.我发现这个例子有助于使用 switch case 语句。 But I think the issue revolves around the action.但我认为问题围绕着行动。

I previous tested this function This worked perfectly fine for me我之前测试过这个 function 这对我来说非常好

let state = {presents : true}

function managePresents(state, action){
    action = {
        type: "presents/increase",};
    switch (action.type) {
        case 'presents/increase':
            return { presents: !state.presents}
        default:
            return state;
    }
}

managePresents(state, action)

Result: {presents: false}结果:{礼物:假}

Any Ideas on how to update the state of the array (friends) using redux?关于如何使用 redux 更新阵列(朋友)的 state 的任何想法?

The issue is that you're trying to do too much with a simple function.问题是你试图用一个简单的 function 做太多事情。 Action is supposed to have two key arguments, TYPE, and PAYLOAD, Not multiple statements that sucked together. Action 应该有两个键 arguments,TYPE 和 PAYLOAD,而不是多个语句吸在一起。

It would help if you tried to run the function Once(manageFriends ) at a time.如果您尝试一次运行 function 一次(manageFriends)会有所帮助。 Or try to use a loop/forEach If you need to run it multiple times.或者尝试使用循环/forEach 如果您需要多次运行它。

 const initialState = { friends: [] }; let action = { type: 'friends/add', payload: { name: 'Mac Miller', hometown: 'Arizona', id: 1, }, }; function manageFriends(state, action) { switch (action.type) { case 'friends/add': return { friends: [...state.friends, action.payload] }; case 'friends/remove': return { friends: state.friends.filter((friend) => friend.id.= action,payload); }: default; return state, } } manageFriends(initialState; action);

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

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