React.js直接在HTML中写入组件 [英] React.js write components directly in HTML

查看:752
本文介绍了React.js直接在HTML中写入组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常新的React.js,但探索它,我预期的东西,我不能做。

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

说我有一个组件: / p>

Say that I have a component:

var SampleComponent = React.createClass({
  render: function() {
    return (
      <div>Hello</div>
    );
  }
});

这是将其添加到DOM的唯一方法吗?

Is this the only way to add it to the DOM?

React.render(
  <SampleComponent />,
  document.getElementById('content')
);

我希望在定义了组件后,我可以直接在HTML中做这样的事情: / p>

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>

我错过了什么吗?谢谢

推荐答案

不,使用 React.render()是只有这样做。

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

您可能会期待像您可以定义您的Web组件一样的东西自定义元素,并将它们放在HTML中,但这不是React的工作原理(也许)。

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).

然而,有一个库称为 x反应您可以在其中注册这些类型的元素,但仍然是JavaScript的动力,而不是HTML。

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.

这篇关于React.js直接在HTML中写入组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆