[React] update react syntaxes and remove legacy methods (#856)

* Update React cheatsheet syntaxes and remove legacy methods

* Update React updated date
This commit is contained in:
Joonas Kallunki 2018-11-16 21:46:54 +02:00 committed by chad d
parent a78dab76cf
commit 4a77b25dee
1 changed files with 74 additions and 28 deletions

102
react.md
View File

@ -4,7 +4,7 @@ category: React
layout: 2017/sheet layout: 2017/sheet
ads: true ads: true
tags: [Featured] tags: [Featured]
updated: 2017-10-10 updated: 2018-10-04
weight: -10 weight: -10
keywords: keywords:
- React.Component - React.Component
@ -48,20 +48,36 @@ ReactDOM.render(<Hello name='John' />, el)
Use the [React.js jsfiddle](http://jsfiddle.net/reactjs/69z2wepo/) to start hacking. (or the unofficial [jsbin](http://jsbin.com/yafixat/edit?js,output)) Use the [React.js jsfiddle](http://jsfiddle.net/reactjs/69z2wepo/) to start hacking. (or the unofficial [jsbin](http://jsbin.com/yafixat/edit?js,output))
### Import multiple exports
{: .-prime}
```jsx
import React, {Component} from 'react'
import ReactDOM from 'react-dom'
```
{: .-setup}
```jsx
class Hello extends Component {
...
}
```
### Properties ### Properties
```html ```html
<Video fullscreen={true} /> <Video fullscreen={true} autoplay={false} />
``` ```
{: .-setup} {: .-setup}
```jsx ```jsx
render () { render () {
this.props.fullscreen this.props.fullscreen
const { fullscreen, autoplay } = this.props
··· ···
} }
``` ```
{: data-line="2"} {: data-line="2,3"}
Use `this.props` to access properties passed to the component. Use `this.props` to access properties passed to the component.
@ -72,7 +88,7 @@ See: [Properties](https://reactjs.org/docs/tutorial.html#using-props)
```jsx ```jsx
constructor(props) { constructor(props) {
super(props) super(props)
this.state = {} this.state = { username: undefined }
} }
``` ```
@ -83,19 +99,30 @@ this.setState({ username: 'rstacruz' })
```jsx ```jsx
render () { render () {
this.state.username this.state.username
const { username } = this.state
··· ···
} }
``` ```
{: data-line="2"} {: data-line="2,3"}
Use states (`this.state`) to manage dynamic data. Use states (`this.state`) to manage dynamic data.
With [Babel](https://babeljs.io/) you can use [proposal-class-fields](https://github.com/tc39/proposal-class-fields) and get rid of constructor
```jsx
class Hello extends Component {
state = { username: undefined };
...
}
```
See: [States](https://reactjs.org/docs/tutorial.html#reactive-state) See: [States](https://reactjs.org/docs/tutorial.html#reactive-state)
### Nesting ### Nesting
```jsx ```jsx
class Info extends React.Component { class Info extends Component {
render () { render () {
const { avatar, username } = this.props const { avatar, username } = this.props
@ -109,15 +136,20 @@ class Info extends React.Component {
As of React v16.2.0, fragments can be used to return multiple children without adding extra wrapping nodes to the DOM. As of React v16.2.0, fragments can be used to return multiple children without adding extra wrapping nodes to the DOM.
```jsx ```jsx
class Info extends React.Component { import React, {
Component,
Fragment
} from 'react'
class Info extends Component {
render () { render () {
const { avatar, username } = this.props const { avatar, username } = this.props
return ( return (
<React.Fragment> <Fragment>
<UserAvatar src={avatar} /> <UserAvatar src={avatar} />
<UserProfile username={username} /> <UserProfile username={username} />
</React.Fragment> </Fragment>
) )
} }
} }
@ -140,7 +172,7 @@ See: [Composing Components](https://reactjs.org/docs/components-and-props.html#c
{: data-line="2"} {: data-line="2"}
```jsx ```jsx
class AlertBox extends React.Component { class AlertBox extends Component {
render () { render () {
return <div className='alert-box'> return <div className='alert-box'>
{this.props.children} {this.props.children}
@ -169,7 +201,7 @@ See: [defaultProps](https://reactjs.org/docs/react-component.html#defaultprops)
### Setting default state ### Setting default state
```jsx ```jsx
class Hello extends React.Component { class Hello extends Component {
constructor (props) { constructor (props) {
super(props) super(props)
this.state = { visible: true } this.state = { visible: true }
@ -180,13 +212,24 @@ class Hello extends React.Component {
Set the default state in the `constructor()`. Set the default state in the `constructor()`.
And without constructor using [Babel](https://babeljs.io/) with [proposal-class-fields](https://github.com/tc39/proposal-class-fields).
```jsx
class Hello extends Component {
state = { visible: true }
}
}
```
{: data-line="2"}
See: [Setting the default state](https://reactjs.org/docs/react-without-es6.html#setting-the-initial-state) See: [Setting the default state](https://reactjs.org/docs/react-without-es6.html#setting-the-initial-state)
Other components Other components
---------------- ----------------
{: .-three-column} {: .-three-column}
### Function components ### Functional components
```jsx ```jsx
function MyComponent ({ name }) { function MyComponent ({ name }) {
@ -204,11 +247,13 @@ See: [Function and Class Components](https://reactjs.org/docs/components-and-pro
### Pure components ### Pure components
```jsx ```jsx
class MessageBox extends React.PureComponent { import React, {PureComponent} from 'react'
class MessageBox extends PureComponent {
··· ···
} }
``` ```
{: data-line="1"} {: data-line="3"}
Performance-optimized version of `React.Component`. Doesn't rerender if props/state hasn't changed. Performance-optimized version of `React.Component`. Doesn't rerender if props/state hasn't changed.
@ -222,6 +267,7 @@ this.forceUpdate()
```jsx ```jsx
this.setState({ ... }) this.setState({ ... })
this.setState(state => { ... })
``` ```
```jsx ```jsx
@ -257,9 +303,8 @@ Add DOM event handlers, timers (etc) on `componentDidMount()`, then remove them
| Method | Description | | Method | Description |
| --- | --- | | --- | --- |
| `componentWillReceiveProps` *(newProps)* | Use `setState()` here | | `componentDidUpdate` *(prevProps, prevState, snapshot)* | Use `setState()` here, but remember to compare props |
| `shouldComponentUpdate` *(newProps, newState)* | Skips `render()` if returns false | | `shouldComponentUpdate` *(newProps, newState)* | Skips `render()` if returns false |
| `componentWillUpdate` *(newProps, newState)* | Can't use `setState()` here |
| `render()` | Render | | `render()` | Render |
| `componentDidUpdate` *(prevProps, prevState)* | Operate on the DOM here | | `componentDidUpdate` *(prevProps, prevState)* | Operate on the DOM here |
@ -274,7 +319,7 @@ DOM nodes
### References ### References
```jsx ```jsx
class MyComponent extends React.Component { class MyComponent extends Component {
render () { render () {
return <div> return <div>
<input ref={el => this.input = el} /> <input ref={el => this.input = el} />
@ -295,7 +340,7 @@ See: [Refs and the DOM](https://reactjs.org/docs/refs-and-the-dom.html)
### DOM Events ### DOM Events
```jsx ```jsx
class MyComponent extends React.Component { class MyComponent extends Component {
render () { render () {
<input type="text" <input type="text"
value={this.state.value} value={this.state.value}
@ -323,7 +368,7 @@ See: [Events](https://reactjs.org/docs/events.html)
{: .-setup} {: .-setup}
```jsx ```jsx
class VideoPlayer extends React.Component { class VideoPlayer extends Component {
render () { render () {
return <VideoEmbed {...this.props} /> return <VideoEmbed {...this.props} />
} }
@ -363,7 +408,7 @@ JSX patterns
### Style shorthand ### Style shorthand
```jsx ```jsx
var style = { height: 10 } const style = { height: 10 }
return <div style={style}></div> return <div style={style}></div>
``` ```
@ -385,7 +430,7 @@ See: [Dangerously set innerHTML](https://reactjs.org/tips/dangerously-set-inner-
### Lists ### Lists
```jsx ```jsx
class TodoList extends React.Component { class TodoList extends Component {
render () { render () {
const { items } = this.props const { items } = this.props
@ -403,19 +448,20 @@ Always supply a `key` property.
### Conditionals ### Conditionals
```jsx ```jsx
<div> <Fragment>
{showMyComponent {showMyComponent
? <MyComponent /> ? <MyComponent />
: <OtherComponent />} : <OtherComponent />}
</div> </Fragment>
``` ```
### Short-circuit evaluation ### Short-circuit evaluation
```jsx ```jsx
<div> <Fragment>
{showPopup && <Popup />} {showPopup && <Popup />}
</div> ...
</Fragment>
``` ```
New features New features
@ -444,10 +490,10 @@ render () {
render () { render () {
// Fragments don't require keys! // Fragments don't require keys!
return ( return (
<React.Fragment> <Fragment>
<li>First item</li> <li>First item</li>
<li>Second item</li> <li>Second item</li>
</React.Fragment> </Fragment>
) )
} }
``` ```
@ -471,7 +517,7 @@ See: [Fragments and strings](https://reactjs.org/blog/2017/09/26/react-v16.0.htm
### Errors ### Errors
```js ```js
class MyComponent extends React.Component { class MyComponent extends Component {
··· ···
componentDidCatch (error, info) { componentDidCatch (error, info) {
this.setState({ error }) this.setState({ error })