我对React.js很新,但是我正在探索它,我期待一些我无法做到的事情。
假设我有一个组件:
var SampleComponent = React.createClass({ render: function() { return ( <div>Hello</div> ); } });这是将它添加到DOM的唯一方法吗?
React.render( <SampleComponent />, document.getElementById('content') );我希望在定义组件后,我可以直接在HTML中执行此类操作:
<!DOCTYPE html> <html lang="en"> <head></head> <body> <SampleComponent /> </body> </html>我错过了什么吗? 谢谢
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.createClass({ render: function() { return ( <div>Hello</div> ); } });Is this the only way to add it to the DOM?
React.render( <SampleComponent />, document.getElementById('content') );I hoped I would be able to do something like this directly in HTML after having defined the component:
<!DOCTYPE html> <html lang="en"> <head></head> <body> <SampleComponent /> </body> </html>Am I missing something? Thanks
最满意答案
不,使用React.render()是“唯一的方法”。
您可能期望Web组件之类的东西,您可以在其中定义自定义元素并将其放在HTML中,但这不是React工作的方式(可能)。
然而,有一个名为x-react的库,您可以在其中注册这些类型的元素,但它仍然是JavaScript驱动的,而不是直接的HTML。
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.
更多推荐
发布评论