React Native 中 Redux Toolkit createSlice 函数出现错误(EXPO)

问题描述 投票:0回答:1

我在 Expo 项目中遇到问题,出现以下错误:

 TypeError: 0, _toolkit.createSlice is not a function (it is undefined), js engine: hermes
    at ContextNavigator (http://10.0.2.2:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.reportAdd.app&modulesOnly=false&runModule=true:153417:24)
    at ExpoRoot (http://10.0.2.2:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.reportAdd.app&modulesOnly=false&runModule=true:153388:30)
    at App
    at withDevTools(App) (http://10.0.2.2:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.reportAdd.app&modulesOnly=false&runModule=true:132684:27)
    at ErrorToastContainer (http://10.0.2.2:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.reportAdd.app&modulesOnly=false&runModule=true:132585:24)
    at ErrorOverlay
    at RCTView
    at View (http://10.0.2.2:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.reportAdd.app&modulesOnly=false&runModule=true:40235:43)
    at RCTView
    at View (http://10.0.2.2:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.reportAdd.app&modulesOnly=false&runModule=true:40235:43)
    at AppContainer (http://10.0.2.2:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.reportAdd.app&modulesOnly=false&runModule=true:40075:36)
    at main(RootComponent) (http://10.0.2.2:8081/index.bundle//&platform=android&dev=true&minify=false&app=com.reportAdd.app&modulesOnly=false&runModule=true:121767:28)

详情:

环境:世博会(SDK 49) 错误消息:TypeError:0,_toolkit.createSlice不是函数(未定义),js引擎:hermes。

Redux 软件包已安装 "react-redux": "^9.0.4", "redux-persist": "^6.0.0", “@reduxjs/toolkit”:“^2.0.1”,

导致错误的文件

import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { RootState } from "../store";

type initialStateTypes = {
  notificationToken: string;
  hasCreatedAccount: boolean;
  currentPhoneNumber?: string;
};


const initialState: initialStateTypes = {
  notificationToken: "",
  hasCreatedAccount: false,
  currentPhoneNumber: "",
};

const appSlice = createSlice({
  name: "app",
  initialState,
  reducers: {

    setNotificationToken: (state, action: PayloadAction<string>) => {
      state.notificationToken = action.payload;
    },

    setHasCreatedAccount: (state, action: PayloadAction<boolean>) => {
      state.hasCreatedAccount = action.payload;
    },

    setCurrentPhoneNumber: (state, action) => {
      state.currentPhoneNumber = action.payload;
    },

    clear() {
      // Note that this should be left intentionally empty.
      // Clearing redux state and localForage happens in rootReducer.ts.
    },
  },
});

// Extract the action creators object and the reducer.
const { actions, reducer } = appSlice;

// Extract and export each action creator by name.
export const {
  setNotificationToken,
  setHasCreatedAccount,
  setCurrentPhoneNumber,
  clear,
} = actions;


export const selectNotificationToken = (state: RootState) =>
  state.app.notificationToken;
  
export const selectHasCreatedAccount = (state: RootState) =>
  state.app.hasCreatedAccount;


  // Export the reducer, either as a default.
export default reducer;
react-native redux expo redux-toolkit
1个回答
0
投票
  • 经过一周的研究和分析,我终于找到了解决方案。我希望它可以节省某人的时间。该 错误是由于处理依赖项常见的 .cjs
     扩展的 Metro 捆绑程序问题导致的。不幸的是,
    .cjs
    并不是唯一的,还有
    .mjs
    等等
  • 就我而言,
  • .cjs
    正在发挥作用。 
    解决方案是让我的metro.config.js
    文件如下所示:
// Learn more https://docs.expo.io/guides/customizing-metro const { getDefaultConfig } = require("@expo/metro-config"); /** @type {import('expo/metro-config').MetroConfig} */ const config = getDefaultConfig(__dirname); // What I had initially, was affecting `getReactNativePersistence`-import { // initializeAuth, getReactNativePersistence } from "firebase/auth"; as well // config.resolver.assetExts.push("cjs"); //Fixes // config.resolver.sourceExts.push("cjs"); // config.resolver.sourceExts = process.env.RN_SRC_EXT // ? [ // ...process.env.RN_SRC_EXT.split(",").concat(config.resolver.sourceExts), // "cjs", // ] // <-- cjs added here // : [...config.resolver.sourceExts, "cjs"]; // <-- cjs added here config.resolver.sourceExts.push[("js", "json", "ts", "tsx", "cjs")]; module.exports = config;

    尝试一下修复评论下的
  • 评论部分,看看什么对你有用。 如果上述方法不起作用,有一些建议的解决方案:
    • 链接1
    • 链接2
  • 祝一切顺利👍🏾

© www.soinside.com 2019 - 2024. All rights reserved.