简体   繁体   English

函数内部的渲染组件

[英]Render component inside a function react

class SurveyQuestion extends React.Component {
  constructor(props) {
    model.onComplete.add(function(result) {
      return <TestEmpty message="test" />;
    }
  });
}

I am new to react, apologize if this is basic question, Is there any chance that if I could return component inside the constructor function, please? 我是新来的人,如果这是一个基本问题,我会道歉,请问是否有可能在构造函数中返回组件? When it is completed then return component. 完成后,返回组件。

I tried with my code, it is not working and no error message. 我尝试使用我的代码,它不起作用,也没有错误消息。

  1. you cannot return component from constructor. 您不能从构造函数返回组件。
  2. In your case you use statefull component SurveyQuestion. 在您的情况下,请使用全状态组件SurveyQuestion。 In this case before to use it you need instantiate a parent class Component which you inherit(extands) by passing a super(props) invoking parent class constructor first inside of your component constructor. 在这种情况下,在使用它之前,您需要实例化一个父类组件,该组件通过在组件构造函数内部首先传递一个调用父类构造函数的super(props)来继承(扩展)组件。
  3. In statefull component you can rewrite your function beyond the constructor using this.state option of React if you want to track a change a statement of the properties by event(put it inside of simple eventHandler) 在statefull组件中,如果您想通过事件跟踪属性的更改(可以将其放入简单的eventHandler中),则可以使用React的this.state选项重写功能,使其超出构造函数的范围。
  4. Statefull component expects from you a render method. Statefull组件期望您提供一个render方法。 So your return method will be inside of it. 因此,您的return方法将在其中。 And of course don't forget about import statement anywhere you need it.;) PS 当然,不要在任何需要的地方忘记导入语句。
  5. You could use a stateless component either. 您也可以使用无状态组件。 Which could be more convinient for you. 这对您来说更方便。 Check it out here https://facebook.github.io/react/docs/components-and-props.html 在这里查看https://facebook.github.io/react/docs/components-and-props.html

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

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