如何获取自定义React Babel编译指示以将字符串作为参数而不是函数传递 [英] How to get custom React Babel pragma to pass string as argument rather than function

查看:374
本文介绍了如何获取自定义React Babel编译指示以将字符串作为参数而不是函数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为对的后续操作创建您自己的Babel处理器替代反应" ,我收到此错误:

As a follow up to How to create your own babel processor alternative to `React`, I am getting this error:

react-dom.development.js:506警告:此浏览器无法识别该标签.如果要渲染React组件,请以大写字母开头.

react-dom.development.js:506 Warning: The tag is unrecognized in this browser. If you meant to render a React component, start its name with an uppercase letter.

  • 在foo
  • 未知
  • 在酒吧
  • in foo
  • in Unknown
  • in Bar

如果我将其设为大写字母,则出现此错误:

If I make it a capital letter, then this error:

未捕获的ReferenceError:未定义Foo

Uncaught ReferenceError: Foo is not defined

我猜这是因为它像这样传递函数:

I'm guessing this is because it's passing it as a function like this:

React.createElement(Foo)

我的问题是,如何使它作为字符串传递?

My question is, how to get it to pass it as a string?

MyThing.create('Foo')

我想知道如何同时使用大写和小写字母.

I would like to know how to do it with both capital and lowercase letters.

推荐答案

据我所知,您将必须修改)标识的功能.该插件未列出用于更改该行为的任何配置选项,因此,如果需要它,则必须派生插件并对其进行修改,使其以您希望的方式工作.

As far as I know, you'll have to modify @babel/plugin-transform-react-jsx to do that. By default, it assumes initially-capped tags are components (functions/classes), and lower-case tags are HTML tag names. It passes components directly to the function identified by the pragma configuration parameter (React.createElement(Foo) by default, for instance) but HTML tag names as strings (React.createElement("div")). The plugin doesn't list any configuration option for altering that behavior, so if you want it, you'll have to fork the plugin and modify it to work the way you want it to work.

这篇关于如何获取自定义React Babel编译指示以将字符串作为参数而不是函数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆