将文本推送到 Redux 中的数组

Pushing text to an array in Redux

我正在尝试使用 Redux 将一些文本推送到数组,但我卡住了。在复制我的状态后,我不确定该怎么做。另外,只想确认我应该将我的 appState 导入到我的减速器中。

store.js

import {createStore} from 'redux';
import rootReducer from './reducers/index';

export const appState = {
    links: []
};

const store = createStore(rootReducer, appState);

export default store;

reducers/index.js

import {appState} from '../store';

function addLink(state = appState, action) {
    switch(action.type) {
        case 'ADD_LINK': 
            const linkName = action.linkName;
            console.log('Adding link');
            console.log(linkName);
            console.log(appState);
            return {
                ...state.splice(),
                // Now what?

            };
        default: 
            return state;
    }
};

export default addLink;

您不需要导入 appState。假设状态只是一个数组,你的方法应该如下所示。

function addLink(state = {links: []}, action) {
    switch(action.type) {
        case 'ADD_LINK': 
            const linkName = action.linkName;
            console.log('Adding link');
            console.log(linkName);
            console.log(appState);
            return {
               ...state,
               links: [linkName, ...state.links]
            };
         default: 
            return state;
    }
};