I am confused whats the difference between a component and a react class?
And when do I use a component over a react class? Looks like a component is a class and createClass creates a component.
https://facebook.github.io/react/docs/top-level-api.html
React.Component
This is the base class for React Components when they're defined using ES6 classes. See Reusable Components for how to use ES6 classes with React. For what methods are actually provided by the base class, see the Component API.
React.createClass
Create a component class, given a specification. A component implements a render method which returns one single child. That child may have an arbitrarily deep child structure. 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.
As of React 15.5, createClass is deprecated. You'll get warnings in the console if you're using it in your code – and, when React 16 comes out, createClass will be removed entirely.
React. createClass allows you to generate component "classes." Under the hood, your component class is using a bespoke class system implemented by React. With ES6, React allows you to implement component classes that use ES6 JavaScript classes.
A React element is an object representation of a DOM node. A component encapsulates a DOM tree. Elements are immutable i,e once created cannot be changed. The state in a component is mutable.
If you don't use ES6 yet, you may use the create-react-class module instead: var createReactClass = require('create-react-class'); var Greeting = createReactClass({ render: function() { return <h1>Hello, {this.props.name}</h1>; } }); The API of ES6 classes is similar to createReactClass() with a few exceptions.
The only React.createClass
functionality that isn't supported by MyComponent extends React.Component
is mixins.
to do getInitialState()
you can do:
class MyComponent extends React.Component {
constructor(props, context) {
super(props, context);
// initial state
this.state = {
counter: 0
};
}
...
}
or if you use a transpiler like babel, you can get
class MyComponent extends React.Component {
state = {
counter: 0
}
...
}
Instead of auto-binding provided by createClass, you could explicitly bind using .bind(this)
like you have shown above, or use the fat arrow ES6 syntax:
class MyComponent extends React.Component {
onClick = () => {
// do something
}
...
}
Instead of putting things in componentWillMount, you could put things in constructor like so:
class MyComponent extends React.Component {
constructor(props, context) {
super(props, context);
// what you would have put in componentWillMount
}
...
}
There are way more details in the React documentation themselves, but basically the only additional functionality that React.createClass buys is mixins, but afaik anything you could have done with mixins can be done with context and higher ordered components.
There are 2 ways of doing the same thing.
React.createClass
is a function that returns a Component class.
MyComponent = React.createClass({
...
});
React.Component
is an existing component that you can extend. Mainly useful when using ES6.
MyComponent extends React.Component {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With