You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native TypeScript模板中Redux状态管理实现报错:Export namespace需先经@babel/plugin-proposal-export-namespace-from转换

解决React Native + TypeScript中Redux导出命名空间的TransformError问题

我之前也碰到过一模一样的问题,其实核心原因是:React Native默认的Babel配置没有启用对export * as这个ES语法提案的支持,而你之前的React Web项目(比如Create React App)默认已经在Babel预设里包含了对应的插件,所以能正常运行。下面是具体的解决步骤:

  • 第一步:安装所需的Babel插件
    打开终端,在项目根目录执行以下命令安装插件:

    # 使用npm
    npm install --save-dev @babel/plugin-proposal-export-namespace-from
    
    # 或者使用yarn
    yarn add --dev @babel/plugin-proposal-export-namespace-from
    
  • 第二步:修改Babel配置文件
    找到项目根目录的babel.config.js(如果没有就新建一个),在plugins数组中添加刚安装的插件:

    module.exports = {
      presets: ['module:metro-react-native-babel-preset'],
      plugins: [
        '@babel/plugin-proposal-export-namespace-from'
      ]
    };
    
  • 第三步:检查TypeScript配置(可选但推荐)
    确保你的tsconfig.json的compilerOptions中,target版本至少是ES2020(因为export * as是ES2020的语法):

    {
      "compilerOptions": {
        "target": "ES2020",
        // 其他你的配置项...
      }
    }
    
  • 第四步:重启Metro服务器
    Babel配置修改后需要重启Metro才能生效,关掉当前运行的Metro服务,重新执行启动命令:

    npx react-native start
    # 或者
    yarn start
    

做完这些步骤,那个TransformError应该就会消失了,你的Redux actionCreators导出也能正常工作啦~

内容的提问来源于stack exchange,提问作者abdemirza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 19:59:06