首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react redux升级到版本8

react redux升级到版本8
EN

Stack Overflow用户
提问于 2022-06-08 10:24:50
回答 1查看 1.4K关注 0票数 0

我将我的react应用程序升级到18版,然后我将从7.2.0升级到8版。

Requests.js Uncaught :无法读取空属性(读取‘数据’)

当我登录时,我会从响应中获取数据,但它不会保存在我的商店中。

代码语言:javascript
复制
import {  createStore, applyMiddleware } from "redux";
import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
import thunk from "redux-thunk";
import reducer from "../reducers/combiner";


const persistConfig = {
   key: "app",
   storage,
     };

const persistedReducer = persistReducer(persistConfig, reducer);

 const store = createStore(
  persistedReducer,
  applyMiddleware(thunk)
 );
 const persistor = persistStore(store);

 export { store, persistor };

我的依赖关系:

代码语言:javascript
复制
"dependencies": {
"@reduxjs/toolkit": "^1.8.2",
"bootstrap": "^5.1.3",
"crypto-js": "^4.1.1",
"js-base64": "^3.7.2",
"leaflet": "^1.8.0",
"leaflet-routing-machine": "^3.2.12",
"node-sass": "^7.0.1",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-leaflet": "^4.0.0",
"react-redux": "^8.0.2",
"react-router-dom": "^6.3.0",
"react-scripts": "^5.0.1",
"redux": "^4.2.0",
"redux-persist": "^6.0.0",
"redux-thunk": "^2.4.1",
"reselect": "^4.1.5",
"typescript": "^4.7.2",
"uuid": "^3.4.0"

},

谢谢你提前..。

这是我使用数据的地方:

代码语言:javascript
复制
  useEffect(() => {
const authKey = localStorage.getItem("auth");
if (authKey === null) {
  navigate("/login");
} else {
  if (window.innerWidth <= 767) {
    window.scrollTo({ top: 600, behavior: "smooth" });
  }
  if (userInfo !== null && userInfo.data && userInfo.data.order_id !== 0) {
    navigate("/driver");
  } else if (latLon.lat === 35.6892 && latLon.lng === 51.389) {
    dispatch(sendToMapIR(latLon.lat, latLon.lng));
    dispatch(deleteRestAddress());
  }
}
}, []);

这就是我的行动:

代码语言:javascript
复制
export const newRequest = (
  userId,
  lat,
  lng,
  address,
  pickedItems,
  total,
  checked,
  restAddress
 ) => {
  return async dispatch => {
  dispatch({ type: NEW_REQUSET_WAITING });
  const result = await fetchNewRequest(
  userId,
  lat,
  lng,
  address,
  pickedItems,
  total,
  checked,
  restAddress
  );
   if (result.success) {
     return dispatch({
     type: NEW_REQUSET,
     payload: result.data
     });
    } else {
      return dispatch({
      type: NEW_REQUSET_FAILED
      });
     }
   };
  };
EN

回答 1

Stack Overflow用户

发布于 2022-06-09 15:13:51

我不知道userInfo对象是什么样子,但也许使用可选链可能会有所帮助:

代码语言:javascript
复制
if (userInfo !== null && userInfo?.data && userInfo?.data.order_id !== 0) {
    navigate("/driver");
  }

如果没有,那么尝试console.log userInfouserInfo.data并检查它是如何的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72544059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档