调用react component-element作为变量



是否有可能调用一个内部有变量的React组件元素?

import React from "react"
/*React functional component*/
function someName() {
const someVar = "componentName"; //the name of the called component
return(
<{someVar}/>
)
}
export default someName;

我尝试在路由器中实现这一点,并使用useState从获取的数据动态地更改文件名(站点)(在元素中)。

我愿意接受各种帮助:)

没有直接的方法,但是你可以使用这个方法。

import ComponentA from '...path';
import ComponentB from '...path';
...
const components = {
componentA: ComponentA,
componentB: ComponentB,
...
}
...
function App(props) {
const TargetComponent = components[props.componentName];
return <TargetComponent />;
}

最新更新