简体   繁体   English

React.js:使用React.render()和React.createClass()的渲染传递子组件之间的区别?

[英]React.js: Difference between passing in the subcomponents with React.render() and with React.createClass()'s render?

I'm working on a project using React.js, and very confused about the composition of React. 我正在使用React.js开发一个项目,并对React的组成非常困惑。

http://facebook.github.io/react/docs/multiple-components.html http://facebook.github.io/react/docs/multiple-components.html

the link above gives an example. 上面的链接给出了一个例子。 It uses React.creatClass() create three components. 它使用React.creatClass()创建三个组件。 A parent component and two child components. 父组件和两个子组件。 The parent component includes the others within it's JSX in the render method. 父组件在render方法中包含JSX中的其他组件。

This example's very clear, but not very 'reusable'. 这个例子很清楚,但不是很“可重用”。 What if I wanna pass in another child in another situation? 如果我想在另一种情况下传给另一个孩子怎么办? React.js seems lacking the 'extend' method like Backbone's view. React.js似乎缺乏像Backbone视图那样的“扩展”方法。

later, I found that you can pass children components in the React.render(), and use this.props.children to composite. 后来,我发现你可以在React.render()中传递子组件,并使用this.props.children进行复合。

  var Tom = React.createClass({
  render: function(){
      return(
          <a>This is Tom.</a>
      )
  }
  });

var John = React.createClass({
  render: function(){
      return(
          <a>This is John.</a>
      )
  }
});

var Outter = React.createClass({
componentDidMount:function(){
  console.log(this.props.children);
},

render: function(){
  return(
      <div className="test">
        {this.props.children}
      </div>
  )
}
});

React.render(<Outter><Tom /><John /></Outter>, document.getElementById('main'));

I think that's great but what's the really difference between this method and the example above? 我认为这很好但是这个方法和上面例子之间的真正区别是什么? is this method the right way to composite components in React.js? 这种方法是在React.js中复合组件的正确方法吗?

thanks 谢谢

The difference is just like you said, that you can pass any components you'd like as children of that component. 区别就像你说的那样,你可以传递你想要的任何组件作为该组件的子级。 Components that use this.props.children are usually components that acts as wrappers for style and behaviour, but the contents of the component changes for different use cases. 使用this.props.children的组件通常是充当样式和行为包装的组件,但组件的内容会针对不同的用例而更改。 Like a popup, where you want the same look and behaviour (like a close button) for every popup, but the contents of the popup is different for every popup. 就像弹出窗口一样,您希望每个弹出窗口都具有相同的外观和行为(如关闭按钮),但弹出窗口的内容对于每个弹出窗口都是不同的。

Components that don't use this.props.children are more like black boxes, they know everything about how they should be rendered and what child components they need. 不使用this.props.children组件this.props.children是黑盒子,他们知道应该如何呈现它们以及它们需要什么子组件。 But you can still make them dynamic by passing other props to them. 但是你仍然可以通过传递其他道具来使它们变得动态。

Components can be also be passed as props. 组件也可以作为道具传递。 Components are just JS objects, and any JS object can be passed as a prop. 组件只是JS对象,任何JS对象都可以作为prop传递。 But I don't think I've ever seen a use case for doing it. 但我认为我从未见过这样做的用例。 It might make some sense if you have a wrapper component with two or more specific "slots" that should be rendered to. 如果你有一个包含两个或更多应该渲染的特定“槽”的包装器组件,这可能会有所帮助。 Something like: 就像是:

var Wrapper = require('./wrapper');
var Header = require('./title');
var Content = require('./content');
var Footer = require('./footer');

var MyComponent = React.createClass({
  render() {
    return <Wrapper footer={Footer} header={Header} content={Content} />;
  }
});

It depends on your use-case, take this two examples of forms: 这取决于你的用例,采取这两个形式的例子:

  1. a login form - almost all login forms have the same structure, username/email, password, a keep me logged in checkbox, and a login button; 登录表单 - 几乎所有登录表单都具有相同的结构,用户名/电子邮件,密码,保持登录状态复选框和登录按钮; a LoginForm component doesn't need to be further customised in regards to its structure so it's suitable to be used as is LoginForm组件不需要针对其结构进一步定制,因此适合按原样使用
  2. a signup form - now depending on context you might give more or less fields to the user to complete when signing up, and in this case you use props.children 注册表单 - 现在根据上下文,您可以在注册时为用户提供更多或更少的字段,在这种情况下,您使用props.children

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

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