React.js write components directly in HTML

前端 未结 2 1043
别跟我提以往
别跟我提以往 2020-12-21 08:20

I\'m very new to React.js but exploring it I expected something that I\'m not able to do.

Say that I have a component:

var SampleComponent = React.cr         


        
相关标签:
2条回答
  • 2020-12-21 08:47

    No, using React.render() is the "only way" to do it.

    You might be expecting something along the likes of Webcomponents where you can define you custom elements and put those in your HTML but that's not the way React works (yet perhaps).

    There is however a library called x-react where you can register those types of elements, but still it's JavaScript powered and not straight up HTML.

    0 讨论(0)
  • 2020-12-21 08:55

    In a typical app you would have a high-level controller-view component mounted to the DOM this way, but that component would contain a hierarchy of child components in its render method to build up the structure of your interface. This is the idea of composable views which is an advantage of react. These child components can be used directly within the jsx of the parents' render methods in the same way you had hoped, so you really only have to call React.render once on the top-level component.

    0 讨论(0)
提交回复
热议问题