How does React.createClass work?

匿名 (未验证) 提交于 2019-12-03 09:52:54

问题:

var MyComponentClass = React.createClass({   render: function () {     return <h1>Hello world</h1>;   } });  ReactDOM.render(     <MyComponentClass />,      document.getElementById('app') );

So, I have been following some tutorials on ReactJS and I am having a hard time understanding what exactly is going on under the hood of React, especially the above piece of code.

Like, what exactly is the need for the render method inside the createClass method of React's library?

How does the render method get called when ReactDOM.render is called?

I have tried looking through the documentation of ReactJS, but I ran into the same problem where there is no explanation of what is going on underneath the hood. Tried googling the problem, same result. Questions seem to be based around how to do it, rather than what it actually does.

Even if someone could point me in the direction of the right area of the docs, that would help me immensely.

回答1:

This top-level-api document would be good start for beginner.

  1. The need for render method:
    It is required when you used React.createClass.
    Within the render method, you cannot modify component state.
    When it is called, it will return a single child (eg. <div> or component defined by yourself)

  2. How does render get called:
    By default, whenever setState is called, React will re-render all components and sub components.
    However, if you set that shouldComponentUpdate() return false, render method will be completely skipped.

Hope it helps



转载请标明出处:How does React.createClass work?
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!