简体   繁体   English

使用react.js渲染其他组件内部的组件

[英]Render components inside other components with react.js

I'm having some trouble figuring out how to render react components, as children inside other components on the serverside. 我在弄清楚如何渲染反应组件方面遇到一些麻烦,就像服务器端其他组件中的子组件一样。 On the client side in my index.js file I'd do something like: 在客户端的index.js文件中,我将执行以下操作:

React.render(
  <Panel title="Full Time Score">
    <Graph />
  </Panel>, document.getElementById('column-main') 
);

But on the server side I'm not getting any luck, is it possible to do something like this? 但是在服务器端,我没有任何运气,是否可以做这样的事情? or is this some kind of anti-pattern and I should always start off with the one parent component. 还是这是一种反模式,我应该始终从一个父组件开始。

What I'm trying to do is have a reusable panel component (it includes a button which toggles visibility) that I can put any content/components inside. 我想要做的是拥有一个可重复使用的面板组件(它包括一个可切换可见性的按钮),我可以将任何内容/组件放入其中。

On the server, the markup returned by React.{renderToString,renderToStaticMarkup} is the HTML output of your component on the first render. 在服务器上, React.{renderToString,renderToStaticMarkup}返回的标记是组件在第一个渲染器上的HTML输出。 On the client, components can re-render themselves after their componentDidMount lifecycle method has fired. 在客户端上,组件可以在其componentDidMount生命周期方法启动后重新呈现自己。

Make sure that the Panel component renders its children prop on the first render. 确保Panel组件在第一个渲染器上渲染其children prop。

I figured out what I think I should do in the end, or at least what solves my problem. 我想出了我到底应该做什么,或者至少解决了我的问题。 Code: 码:

  var Panel = require('../components/panel.react.jsx');
  var Dataset = require('../components/dataset.probability.react.jsx');

  // Main Column HTML
  var html = React.renderToStaticMarkup(React.DOM.div(null,
    Panel(
      {title:"Title of panel"},

      Graph({data: data})
    ),
    Panel(
      {title:"Title of panel"}

      Graph({data: data})
    ),
    Panel(
      {title:"Title of panel"}

      Graph({data: data})
    )
  ));

  // var html now contains the markup, send it to your template.

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

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