使用脚手架create-react-app搭建React项目时,常见的报错及解决办法汇总。
1、redux.js:473 Uncaught Error: The slice reducer for key "errors" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined.
问题:errors的reducer在初始化时数据返回了undefined,或者没有返回。
解决:
a、reducer没有给与返回值;
b、reducer如果没有返回值可以给与null,不要给与undefined
2、react v18.0函数式组件,在useEffect中进行请求,请求执行两次。
原因:react的严格模式(Strict Mode)。可能不止一次的调用渲染阶段的生命周期。
解决:去掉React.StrictMode。
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
// <React.StrictMode>
<Provider store={store}>
<App />
</Provider>
// </React.StrictMode>
);
解决截图:
复现截图1:
复现截图2: