gpt4 book ai didi

reactjs - React Redux 在 reducer 中分配箭头功能警告

转载 作者:行者123 更新时间:2023-12-04 16:25:36 25 4
gpt4 key购买 nike

谁能告诉我为什么在我的 React 应用程序中收到此警告?

src/reducers/posts.jsLine 3:1: Assign arrow function to a variable before exporting as module default

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;
}
}

最佳答案

您不能同时导出默认值声明变量。试试这个:

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

或使用传统命名函数导出默认值:

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;
}
}

关于reactjs - React Redux 在 reducer 中分配箭头功能警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65032258/

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