Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"]).

So, if I have a string, such as "<MyComponent />", how can I turn into JSX and render?

like image 982
Matt Fordham Avatar asked Mar 16 '23 20:03

Matt Fordham


1 Answers

JSX is just a nice syntax for function calls, so you need to have the actual functions to use a component. If you have an object that contains React components then you can render a component based on a string property. 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(...). Then you can use <MyComponents.SomeInput /> in your JSX.

like image 65
Scott Avatar answered Mar 26 '23 03:03

Scott