This is just to record all the issues I met during React development.
Super
Issue blog: What’s the difference between “super()” and “super(props)” in React when using es6 classes?
1 | // if you want to access `this.props` in constructor |
vs
1 | // If you do not want to access `this.props` in constructor |
DisplayName
SomeComponent.displayName
is just to identify the name of current module.
No functional use, just to help the debug to be clear.
Issue blog: What is React component ‘displayName’ is used for?
React source code during debugging: Debug code in react
1 | function warnIfInvalidElement(Component, element) { |
Context of Method
For the following code snippet:
1 | class IncreaserPanel extends React.Component { |
Tips: Using onClick={() => this.increaser()}
will cause that every time on rendering function, this function will be recreated. Then especially with the case that it is used as a props to child react element, it will lead to rerendering of the child element. This is a waste of performance.
Export function
In ES6 class function grammer, the export
could be like this:1
2
3
4
5
6
7
8
9
10
11
12
13import React from 'react';
class MyComponent extends React.Component {
//...
}
export default MyComponent; // import MyComponent from 'myComponent.js'
// or
/**
* export {
* MyComponent, // import {MyComponent} from 'myComponent.js'
* MySecondComponent,
* //...
* }
*/
In CMD guideline, the export could be written like this:1
2
3
4
5
6
7import React from 'react';
class MyComponent extends React.Component {
//...
}
module.exports = {
MyComponent: MyComponent // import {MyComponent} from 'myComponent.js'
};
Not much difference, but if we use CMD guideline, it is easier to use in Server side render (SSR), because node.js supports CMD guideline.