反应Redux动作

时间:2019-10-30 08:28:39

标签: reactjs redux

我正在使用redux进行反应,当我声明动作时它会给出错误。源代码如下。

src / actions / index.js

const addTask =(task)=>{
    return {
        type:'ADD_TASK',
        payload:task
    };
},
deleteTask=(taskId)=>{
    return {
        type:'DELETE_TASK',
        payload:taskId
    };
};

export default {addTask, deleteTask};

它给出的错误是

编译失败 ./src/components/taskbar/index.js 尝试导入错误:“ ../../ actions / index”未导出“ addTask”。

3 个答案:

答案 0 :(得分:0)

您可以使用命名导出/导入

// Named export
export const addTask =(task)=>{
    return {
        type:'ADD_TASK',
        payload:task
    };
}
// named import
import { addTask } from '../../actions/index'

默认导出/导入:

const addTask =(task)=>{
    return {
        type:'ADD_TASK',
        payload:task
    };
}
// default export
export default { addTask }
// default import
import taskActions from '../../actions/index'
const addTask = taskActions.addTask

答案 1 :(得分:0)

src / reducers / index.js这个有问题吗?是否创建商店或不创建商店?

import {combineReducers} from 'redux';

const tasksReducer =(state=[] , action)=>{
    switch(action.type){
        case 'ADD_TASK':
            state=state.concat(action.payload);
            break;
        case 'DELETE_TASK':
            state=state.slice();
            state.splice(action.payload,1);
            break;
    }
    return state;
},
reducers=combineReducers({
    tasks:tasksReducer
});

export default reducers;

答案 2 :(得分:-1)

您应该尝试

export const addTask =(task)=>{
    return {
        type:'ADD_TASK',
        payload:task
    };
}

export const deleteTask=(taskId)=>{
    return {
        type:'DELETE_TASK',
        payload:taskId
    };
};