我们可以通过调用React.createClass()来创建一个组件类,但是什么是“组件类”呢?
最佳答案
React 的 createClass
是一个返回工厂函数的方法,用于创建具有特定原型(prototype)的组件:
const Box = React.createClass({
render() {
return (<div class="box"></div>);
}
});
虽然这是一个简单的示例,但它表明您稍后可以直接或在 JSX 中按名称引用此“组件类”:
let box = React.createElement(Box); // direct
// in some other component's render method:
<Box />
任一格式都会返回该组件类型的新实例。
<小时/>One thing that makes components different than standard prototypal classes is that you don't need to call
new
on them. They are convenience wrappers that construct backing instances (vianew
) for you.
关于javascript - React.js 中的组件类是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34424188/