0

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

3

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

"which returns a factory function" this is wrong, createClass return a constructor function.
Also, it might be worth to note that <Box /> transpiles to React.createElement(Box).
@Pavlo you don't call it with new, and it returns a new instance every time; I'd call that a factory.
React.createElement used to do it internally, but, surprisingly for me, not anymore: github.com/facebook/react/blob/master/src/isomorphic/classic/…
You actually don't call it at all, just pass around, so I think it doesn't really matter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.