用React完成了好几个小项目, 同时还在学习React Native开发, 记录一些ES6特别便捷的用法和guideline。持续更新…
React conventions
propType and defaultProps
使用propType不仅仅是检查props数据的格式, 也是提醒自己这个组件可以利用的来自母组件的数据, 方便以后开发。
1 | // Title.jsx |
this.props.children
1 | class Title extends React.Component { |
Dependency injection
One disadvantage of using react, is that when we need to pass down data to a deeply-nested component, which requires passing props all along the way.
A simple switcher example
1 | class Switcher extends React.Component { |
Data flow
Basically two way of passing data down with server call. One is parent component make a server request, usually in the componentDidMount
and componentWillMount
, and pass down the data using props to the children components. The disadvantage of that is the verbose passing process.
Another way is allow children components to make request themselves, which is the way of GraphQL.
ES6
Arrow functions
what the heck is the arrow function? Unlike functions, arrows share the same lexical this as their surrounding code.
Modules
Language-level support for modules for component definition. Codifies patterns from popular JavaScript module loaders (AMD, CommonJS). Runtime behaviour defined by a host-defined default loader. Implicitly async model – no code executes until requested modules are available and processed.
1 |
|
Some additional features include export default
and export *
:
1 | // lib/mathplusplus.js |
Deconstructing
1 | let [a, b] = [1, 2]; |
Functional binding
1 | class Link extends Component { |
Adding the function bind syntax to ::this.click converts down to this.click.bind(this), ensuring that your callbacks in a component are called with this scoped to the component.
Spread operator
尤其在React Native的redux框架里, 我们不能在reducer里修改state, 只能每次返回一个新的object, 这时候spread syntax特别方便。
1 | import * as postActions from 'actions/posts'; |
In this example we create a new object containing all values from postActions and userActions in one line.
React interview question
Check more information from this post.
- Under what circumstances would you choose React over (AngularJS, etc)?
- If React only focuses on a small part of building UI components, can you explain some pitfalls one might encounter when developing a large application?
- If you were rewriting an AngularJS application in React, how much code could you expect to re-use?
High-Level Component Lifecycle
At the highest level, React components have lifecycle events that fall into three general categories:
- Initialization
- State/Property Updates
- Destruction
Every React component defines these events as a mechanism for managing its properties, state, and rendered output. Some of these events only happen once, others happen more frequently; understanding these three general categories should help you clearly visualize when certain logic needs to be applied.
For example, components will automatically re-render themselves any time their properties or state change. However in some cases a component might not need to update — so preventing the component from re-rendering might improve the performance of our application.1
2
3
4
5
6
7class MyComponent extends React.Component {
// only re-render if the ID has changed!
shouldComponentUpdate(nextProps, nextState) {
return nextProps.id === this.props.id;
}
}
Example interview coding challenge
1 | class MyComponent extends React.Component { |
However, there are two problems with the code above.
1 | // The constructor does not pass its props to the super class. It should include the following line: |
And in order to use this component, we can do this:
1 | <MyComponent headerText=”A list of paragraph tags”> |