What is React
Props (vs State)
React.createElement()State (vs Props)
Props vs State
React vs ReactDOM
“As we look at packages like react-native, react-art, react-canvas, and react-three, it’s become clear that the beauty and essence of React has nothing to do with browsers or the DOM. To make this more clear and to make it easier to build more environments that React can render to, we’re splitting the main react package into two: react and react-dom.”
React.createElement() and ReactDOM.render()
To render a new tree into the DOM, you create ReactElements and pass them to ReactDOM.render along with a regular DOM element.
var root = React.createElement('div');
ReactDOM.render(root, document.getElementById('example'));React components
You can use React using only ReactElements but to really take advantage of React, you’ll want to use ReactComponents to create encapsulations with embedded state.
var MyComponent = React.createClass({
render: function() {
...
}
});createClass vs ES6 class
You may also define your React classes as a plain JavaScript class. For example using ES6 class syntax
class HelloMessage extends React.Component {
render() {
return <div>Hello {this.props.name}</div>;
}
}
ReactDOM.render(, document.getElementById('someId'));ndlr: Au lieu du code suivant (exemple perso)
var HelloMessage = React.createClass({
render: function() {
return <div>Hello {this.props.name}</div>;
}
});ReactDOM.render(, document.getElementById(‘someId’));
createClass vs ES6 class
React.createClass is the workhorse of React. You can use it to model your components. At minimum it should implement a render method although it can implement various lifecycle hooks.
Alternatively you can use a ES6 type class. It’s more or less equal to createClass except that you cannot use mixins here. I prefer ES6 syntax myself as it hides some of the complexity and makes the API a bit tighter thanks to the usage of ES6 class constructor.
cloneElement()
Refs
The “don’t use refs” sentiment is correct when talking about using them for component instances. Meaning, you shouldn’t use refs as a way to grab component instances and call methods on them. This is the incorrect way to use refs and is when refs go south quickly.
The correct (and very useful) way to use refs is when you’re using them to get some value from the DOM. For example, if you have an input field attaching a ref to that input then grabbing the value later through the ref is just fine. Without this way, you need to go through a fairly orchestrated process for keeping your input field up to date with either your local state or your flux store - which seems unnecessary.
What is the difference between using constructor vs getInitialState
The two approaches are not interchangeable. You should initialize state in the constructor when using ES6 classes, and define the getInitialState method when using React.createClass.
```javascript
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = { /* initial state */ };
}
}
~~~
is equivalent to
```javascript
var MyComponent = React.createClass({
getInitialState() {
return { /* initial state */ };
},
});
~~~
Refs
Refs are used to access the real DOM and not the virtual DOM of react. It’s needed when you need to access the real DOM.
```javascript
this.refs.myInput
ReactDOM.findDOMNode(this.refs.myInput).focus()
ReactDOM.findDOMNode(this.refs.myInput).value
~~~
Reusable Components
When designing interfaces, break down the common design elements (buttons, form fields, layout components, etc.) into reusable components with well-defined interfaces. That way, the next time you need to build some UI, you can write much less code. This means faster development time, fewer bugs, and fewer bytes down the wire.
Props validation
As your app grows it’s helpful to ensure that your components are used correctly. We do this by allowing you to specify propTypes. React.PropTypes exports a range of validators that can be used to make sure the data you receive is valid. When an invalid value is provided for a prop, a warning will be shown in the JavaScript console.
```javascript
React.createClass({
propTypes: {
optionalArray: React.PropTypes.array,
optionalBool: React.PropTypes.bool,
optionalFunc: React.PropTypes.func,
optionalNumber: React.PropTypes.number,
optionalObject: React.PropTypes.object,
optionalString: React.PropTypes.string,
optionalSymbol: React.PropTypes.symbol,
}
})
~~~
Use state, not refs (for a form)
The short version: avoid refs.
They’re bad for maintainability, and lose a lot of the simplicity of the WYSIWYG model render provides.
People think refs are ‘easier’ than keeping it in state. This may be true for the first 20 minutes, it’s not true in my experience after that. Put your self in a position to say “Yeah, I’ll have it done in 5 minutes” rather than “Sure, I’ll just rewrite a few components”.