nextjs + react-native-web + 样式组件:warnOnce

AHe*_*rth 3 styled-components next.js react-native-web

当我尝试将 NextJS 与 react-native-web 和 styled-components 一起使用时,我经常碰壁。

问题似乎与样式组件中“react-native”的不正确别名有关。我不确定如何解决它。

我知道如何让它与 Razzle.js 一起工作,但我一生都无法弄清楚如何使用 NextJS 达到相同的工作状态。我怀疑它与 webpack 的 config.externals 有关 - 但它也可能是 babel.config.js 中的东西。

如果有人解决了这个问题,你将成为我今年最喜欢的人。我已经设置为 repo 重现问题

--- Next.js
pages/index.js - WORKS
pages/problem.js - FAILS (has styled-components/native)

--- Razzle
pages/Home.js - WORKS
pages/Home.js - WORKS (has styled-components/native)
Run Code Online (Sandbox Code Playgroud)

https://github.com/Aleksion/rnw-styled-next https://github.com/Aleksion/rnw-styled-razzle

Hed*_*nto 7

我用谷歌搜索过,也发现了这个案例。解决方案是安装 next-transpile-modules 并像下面这样配置你的 next.config.js

const withTM = require('next-transpile-modules')

module.exports = withTM({
    transpileModules: [
      "react-native", "styled-components", "styled-components/native"
    ],
    webpack: config => {
      return {
        ...config,
        resolve: {
          ...config.resolve,
          extensions: [
            '.web.ts',
            '.web.tsx',
            '.ts',
            '.tsx',
            '.web.js',
            '.web.jsx',
            '.js',
            '.jsx',
            ...config.resolve.extensions
          ],
          alias: {
            ...config.resolve.alias,
            'react-native$': 'react-native-web'
          }
        }
      }
    }
  })
Run Code Online (Sandbox Code Playgroud)

这是你的分叉仓库:https : //github.com/hedikasmanto/rnw-styled-next

结果:

在此处输入图片说明

希望它能拯救你的一天:)