简体   繁体   English

React / JSX动态组件名称

[英]React/JSX dynamic component names

I am looking to render a component based upon a string. 我期待基于字符串渲染组件。 Essentially, I am hoping to find the JSX equivalent to JavaScript's dynamic function name ability ( parent["childMethod"] ). 基本上,我希望找到JSX等效于JavaScript的动态函数名称能力( parent["childMethod"] )。

So, if I have a string, such as "<MyComponent />" , how can I turn into JSX and render? 所以,如果我有一个字符串,比如"<MyComponent />" ,我怎么能变成JSX并渲染?

JSX is just a nice syntax for function calls, so you need to have the actual functions to use a component. JSX只是函数调用的一个很好的语法,因此您需要使用实际函数来使用组件。 If you have an object that contains React components then you can render a component based on a string property. 如果您有一个包含React组件的对象,那么您可以基于字符串属性呈现组件。 For example if you have an object called MyComponents (has to be uppercase for JSX) and that object has React components like MyComponents.SomeInput = React.CreateClass(...) . 例如,如果你有一个名为MyComponents的对象(对于JSX必须是大写的),并且该对象具有像MyComponents.SomeInput = React.CreateClass(...)这样的React组件。 Then you can use <MyComponents.SomeInput /> in your JSX. 然后,您可以在JSX中使用<MyComponents.SomeInput />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM