Render a React component based on its name

前端 未结 2 1702
情歌与酒
情歌与酒 2021-02-08 09:47

Context: I am developing a widget-based webapp, (like the defunct iGoogle, where users can choose which widgets they want to display). Each widget is a React component.

2条回答
  •  失恋的感觉
    2021-02-08 10:31

    You could use an object as a lookup for the component type and keep the details of rendering it in one place:

    var components = {
      'HiWidget': HiWidget,
      'HelloWidget': HelloWidget
    }
    
    var Component = components[dataFromObj.type]
    React.render(
      ,
      document.getElementById('try3')
    )
    

提交回复
热议问题