【问题标题】:React-native with storybook problem react-native-vector-icons and react-native-paperReact-native 与故事书问题 react-native-vector-icons 和 react-native-paper
【发布时间】:2020-12-20 12:22:45
【问题描述】:

我们正在react-native 中开发一个使用 react-native-paper 的组件,因为这也适用于web,我们希望使用 storybook 能够创建也可以在浏览器上测试的基本示例.

但问题是我们得到了以下错误,这些错误不取决于我们,而是取决于我们使用的库。

正如您在错误下方看到的,组件似乎已加载数据。

有人知道如何帮助我们了解确切的错误是什么(以防万一还有如何解决它),也知道如何联系指示的模块以纠正它们。

"dependencies": {
    "react": "16.13.1",
    "react-native": "0.63.1",
    "react-native-paper": "^4.5.0",
    "react-native-vector-icons": "^7.1.0",
    "react-native-web": "^0.13.4",
    "styled-components": "^5.1.1"
  }

【问题讨论】:

  • 您使用的是哪个操作系统?
  • @LeriGogsadze:我用的是mac os,你觉得是os问题吗?
  • 你在 XCode 中粘贴了Fonts 文件夹吗?
  • @LeriGogsadze:不知道我明白,在 ios 上它可以工作:i.stack.imgur.com/l1PVR.png 下面你可以看到故事书控件,它在浏览器上不起作用。

标签: javascript react-native storybook react-native-paper react-native-vector-icons


【解决方案1】:

遇到了同样的问题;我正在制作适用于react-domreact-native 的组件(使用react-native-web)。我还使用 Storybook 单独测试/开发组件。

solution described here 为我工作。

据我了解,问题是由于插件@babel/plugin-proposal-class-properties 在编译过程中未包含在内。

我最终得到了以下.storybook/webpack.config.js;因为我链接的解决方案格式有点错误。

const path = require('path')

module.exports = async ({config, mode}) => {
  config.module.rules.push({
    test: /\.(js|jsx|ts|tsx)$/,
    loader: 'babel-loader',
    include: path.resolve(__dirname, '../node_modules/'),
    options: {
      presets: [
        "@babel/env",
        "@babel/preset-react",
        "module:metro-react-native-babel-preset",
      ],
      plugins: [
        "react-native-web",
        "@babel/plugin-proposal-class-properties"
      ]
    }
  })

  config.module.rules.push({
    test: /\.ttf$/,
    loader: 'url-loader',
    include: path.resolve(__dirname, '../node_modules/react-native-vector-icons/Fontisto.js')
  })

  config.resolve.alias = {
    'react-native': 'react-native-web'
  }

  return config
}

P.S. 我使用 this person's issuerecommended installation for "web" 工作。我的.storybook/preview.js 看起来像这样:

... (export parameters, export globalTypes, etc..)

// ==
import Fontisto from '../node_modules/react-native-vector-icons/Fontisto'
import iconFont from '../node_modules/react-native-vector-icons/Fonts/Fontisto.ttf'

const iconFontStyle =`
  @font-face {
    src: url(${iconFont});
    font-family: Fontisto;
  }
`

const style = document.createElement('style')
style.type = 'text/css'
if (style.styleSheet) {
  style.styleSheet.cssText = iconFontStyle
} else {
  style.appendChild(document.createTextNode(iconFontStyle))
}
document.head.appendChild(style)
//

希望一切顺利!

P.P.S 抱歉,值得一提的是,我不确定这个 Babel 配置是否更好地放在 .storybook/main.js as recommended by Storybook.js 中。我可以让这个方法起作用;并且找不到任何关于我将如何去做的例子;我上面提到的解决方案是第一位的。

【讨论】:

  • 我迷路了,我不知道如何解决它。我正在使用以下样板:github.com/ugglr/react-native-storybook-boilerplate
  • @Paul 很抱歉,希望我没有为此做出太多贡献,哈哈;我将不得不为样板加注星标,谢谢,我无法让 Styled-Components 为我需要的东西工作,所以也许这会有所帮助。不幸的是,它不是 TS。我认为这就是我的问题所在。
  • 那么你是在告诉我这是相反的,也许我帮助了你? ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-05
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 2020-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多