marco
marco

Reputation: 1140

What is a component class in React.js?

We can create a component class by invoking React.createClass(), but what is a "component class"?

Upvotes: 0

Views: 116

Answers (1)

Evan Davis
Evan Davis

Reputation: 36592

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 new on them. They are convenience wrappers that construct backing instances (via new) for you.

Upvotes: 2

Related Questions