有人能告诉我为什么在我的React应用程序中收到此警告吗?
src/reducers/post.js第3:1行:在导出为模块默认之前,将箭头函数分配给变量
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;
}
}
为函数添加变量:
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必须知道您导出的是什么