We can create a component class by invoking React.createClass(), but what is a "component class"?
1 Answer
React's createClass is a method which returns a factory function for creating Components with a specific prototype:
const Box = React.createClass({
render() {
return (<div class="box"></div>);
}
});
While this is a trivial example, it shows that you can later reference this "Component Class" by name either directly or in JSX:
let box = React.createElement(Box); // direct
// in some other component's render method:
<Box />
either format will return a new instance of that component type.
From the React API docs:
One thing that makes components different than standard prototypal classes is that you don't need to call
newon them. They are convenience wrappers that construct backing instances (vianew) for you.
5 Comments
Pavlo
"which returns a factory function" this is wrong,
createClass return a constructor function.Pavlo
Also, it might be worth to note that
<Box /> transpiles to React.createElement(Box).Evan Davis
@Pavlo you don't call it with
new, and it returns a new instance every time; I'd call that a factory.Pavlo
React.createElement used to do it internally, but, surprisingly for me, not anymore: github.com/facebook/react/blob/master/src/isomorphic/classic/…Pavlo
You actually don't call it at all, just pass around, so I think it doesn't really matter.
React.createClasscreates a "class" which extends (and so can be treated as) a Component facebook.github.io/react/docs/top-level-api.html