简体   繁体   English

React Redux 分配箭头 Function 减速器警告

[英]React Redux Assign Arrow Function Warning in reducers

Can someone tell me why I'm getting this Warning in my React App?有人能告诉我为什么我在我的 React 应用程序中收到这个警告吗?

src/reducers/posts.js Line 3:1: Assign arrow function to a variable before exporting as module default src/reducers/posts.js 第 3:1 行:在导出为模块默认值之前将箭头 function 分配给变量

import { FETCH_ALL, CREATE, UPDATE, DELETE } from '../constants/actionTypes';

export default (posts = [], action) => {
    switch (action.type) {
        case FETCH_ALL:
            return action.payload;
        case UPDATE:
            return posts.map((post) => post._id === action.payload._id ? action.payload : post);
        case CREATE:
            return [ ...posts, action.payload];
        case DELETE:
            return posts.filter((post) => post._id !== action.payload);
        default:
            return posts;
    }
}

add variable to your function:将变量添加到您的 function:

export default const variable = (posts = [], action) => {
    switch (action.type) {
        case FETCH_ALL:
            return action.payload;
        case UPDATE:
            return posts.map((post) => post._id === action.payload._id ? action.payload : post);
        case CREATE:
            return [ ...posts, action.payload];
        case DELETE:
            return posts.filter((post) => post._id !== action.payload);
        default:
            return posts;
    }
}

JavaScript have to know what are you exporting JavaScript 必须知道你在出口什么

You cannot export default and declare a variable at the same time.您不能同时导出默认值声明变量 Try this instead:试试这个:

const variable = (posts = [], action) => {
    switch (action.type) {
        case FETCH_ALL:
            return action.payload;
        case UPDATE:
            return posts.map((post) => post._id === action.payload._id ? action.payload : post);
        case CREATE:
            return [ ...posts, action.payload];
        case DELETE:
            return posts.filter((post) => post._id !== action.payload);
        default:
            return posts;
    }
}

export default variable

Or export default with the traditional named function :或者使用传统的命名 function导出默认值:

export default function variable(posts = [], action) {
    switch (action.type) {
        case FETCH_ALL:
            return action.payload;
        case UPDATE:
            return posts.map((post) => post._id === action.payload._id ? action.payload : post);
        case CREATE:
            return [ ...posts, action.payload];
        case DELETE:
            return posts.filter((post) => post._id !== action.payload);
        default:
            return posts;
    }
}

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

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