1
A
回答
1
您可以创建一个导出所有不同组件
// Components.js
export Foo from './Foo'
export Bar from './Bar'
然后将它们导入文件中的所有
import * as Components from './Components'
那么你可以动态地创建他们基于变量/道具:
render() {
// this.props.type = 'Foo'
// renders a Foo component
const Component = Components[this.props.type];
return Component && <Component />;
}
3
您需要引用存储到动态组件:
import ComponentName from 'component-name'
class App extends Component {
constructor(props) {
super(props)
this.components = {
'dynamic-component': ComponentName
}
}
render() {
// notice capitalization of the variable - this is important!
const Name = this.components[this.props.componentName];
return (
<div>
<Name />
</div>
)
}
};
render(<App componentName={ 'dynamic-component' } />, document.getElementById('root'));
更多信息,请参见react docs。
0
Okay, for me this worked:
import {Hello} from 'ui-hello-world';
let components = {};
components['Hello'] = Hello;
export default components;
然后在我的课上: 从'..... json'导入customComps; .... let Component = Custom.default [customComps.componentName];
return (
<Component
相关问题
- 1. 动态添加反应组件
- 2. 动态添加图像反应Webpack
- 3. 动态加载反应组件
- 4. 如何动态添加更多组件反应本机
- 5. 动态成分反应
- 6. 加载静态SVG文件反应并添加动态式样
- 7. 添加部分与动态数组
- 8. 在反应应用程序中动态添加样式属性
- 9. 动态呈现组件在反应V15
- 10. 反应的组分状态VS道具动态输入
- 11. 反应原生参考不工作在动态添加行
- 12. e.target.value在添加动态输入数据时反应不明确
- 13. 动态添加可滚动选项卡mdl反应?
- 14. 绑定反应组分Redux的状态
- 15. Zenject动态添加组件
- 16. 添加UI组件动态
- 17. 动态添加gui组件?
- 18. 动态添加Primefaces组件
- 19. 动态添加组合框
- 20. 动态添加组织
- 21. 动态添加组件
- 22. 动态加载分组tableView
- 23. 添加成员动态反对
- 24. 无状态组件中的反应添加/删除类onScroll
- 25. 动态添加组到列表视图不反映后notifyDataSetChanged
- 26. 添加分解反应路由器
- 27. 添加“<script>”反应成分
- 28. 添加装载机反应成分
- 29. 动态反应选择菜单组件
- 30. 创建动态组件反应(本机)
参考此答案https://stackoverflow.com/questions/36941829/how-to-load-component-dynamically-in-reactjs/46188808#46188808 –