简体   繁体   English

react.js 在客户端渲染 React 组件

[英]react.js rendering react components on client side

I am using node.js with react.js as my templating library.我使用 node.js 和 react.js 作为我的模板库。 I am able to render my components on server side by using node's res.render(..), but I want to make my components dynamic by rendering them on client side using react.js and calling the onclick handlers.我可以使用 node 的 res.render(..) 在服务器端呈现我的组件,但我想通过使用 react.js 在客户端呈现它们并调用 onclick 处理程序来使我的组件动态化。 I have my components defined in .jsx format.我的组件以 .jsx 格式定义。 How can I interact with the browser in client side and render my react components dynamically?如何与客户端的浏览器交互并动态呈现我的 React 组件?

You will need to have your code transpiled and bundled, then you can insert it in a script tag just like any other script.您需要对代码进行转译和捆绑,然后您可以像任何其他脚本一样将其插入到脚本标记中。 One popular option is webpack;一种流行的选择是 webpack; another is the use of Browserify with babelify in a gulp task.另一个是在 gulp 任务中使用 Browserify 和 babelify。

You can start using it with ES6 class syntax using babel js.您可以使用 babel js 开始将它与 ES6 类语法一起使用。 I have created a sample component here我在这里创建了一个示例组件

You can also use react-starter-kit which gives you all the setup pre-loaded.您还可以使用react-starter-kit为您提供所有预加载的设置。 A good place to start would be Facebook's react documentation一个好的起点是 Facebook 的 react文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM