React.js直接在HTML中编写组件(React.js write components directly in HTML)

编程入门 行业动态 更新时间:2024-10-23 07:36:04
React.js直接在HTML中编写组件(React.js write components directly in HTML)

我对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.

更多推荐

本文发布于:2023-08-06 06:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446092.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组件   js   React   HTML   components

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!