// x ** y
let squared = 2 ** 2;
// same as: 2 * 2
let cubed = 2 ** 3;
// same as: 2 * 2 * 2
// x **= y
let a = 2;
a **= 2;
// same as: a = a * a;
let b = 3;
b **= 3;
// same as: b = b * b * b;
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
x; // 1
y; // 2
z; // { a: 3, b: 4 }
let n = { x, y, ...z };
n; // { x: 1, y: 2, a: 3, b: 4 }
// modueA.js
const moduleA = 'Hello';
export { moduleA };
// App.js
import React, { Component } from 'react';
class App extends Component {
handleClick = () => {
import('./moduleA')
.then(({ moduleA }) => {
// Use moduleA
})
.catch(err => {
// Handle failure
});
};
render() {
return (
);
}
}
export default App;
//before
class DataGrid extends React.Component {
constructor() {
super();
this.state = {
items: [],
};
...
}
DataGrid.propTypes = {
options: PropTypes.objectOf(PropTypes.any),
columns: PropTypes.arrayOf(PropTypes.object),
rows: PropTypes.arrayOf(PropTypes.object),
events: PropTypes.arrayOf(PropTypes.func),
};
DataGrid.defaultProps = {
options: {
rowHeight: 25,
forceFitColumns: true,
},
columns: [],
rows: [],
events: [],
};
// after
class DataGrid extends React.Component {
static propTypes = {
options: PropTypes.objectOf(PropTypes.any),
columns: PropTypes.arrayOf(PropTypes.object),
rows: PropTypes.arrayOf(PropTypes.object),
events: PropTypes.arrayOf(PropTypes.func),
};
static defaultProps = {
options: {
rowHeight: 25,
forceFitColumns: true,
},
columns: [],
rows: [],
events: [],
};
state = {
items: [],
};
}
// before
function asyncTask () {
return functionA()
.then((valueA) => functionB(valueA))
.then((valueB) => functionC(valueB))
.then((valueC) => functionD(valueC))
.catch((err) => logger.error(err))
}
// after
async function asyncTask () {
try {
const valueA = await functionA()
const valueB = await functionB(valueA)
const valueC = await functionC(valueB)
return await functionD(valueC)
} catch (err) {
logger.error(err)
}
}
They are all yours!
npm -g create-react-app
create-react-app my-app
React + Redux + React Router
function Welcome(props) {
return Hello, {props.name}
;
}
class Welcome extends React.Component {
render() {
return Hello, {this.props.name}
;
}
}
Presentational Components | Container Components | |
---|---|---|
Purpose | How things look (markup, styles) | How things work (data fetching, state updates) |
Aware of Redux | No | Yes |
To read data | Read data from props | Subscribe to Redux state |
To change data | Invoke callbacks from props | Dispatch Redux actions |
Are written | By hand | Usually generated by React Redux |
{
domainData1 : {},
domainData2 : {},
appState1 : {},
appState2 : {},
ui : {
uiState1 : {},
uiState2 : {},
}
}
import { BrowserRouter as Router } from 'react-router-dom';
<Router>
<Route path="/" component={App} />
</Router>
<Route path="/user/:username" component={User}/>
const User = ({ match }) => {
return Hello {match.params.username}!
}
<Route path="/home" render={() => Home}/>
{
key: 'ac3df4', // not with HashHistory!
pathname: '/somewhere'
search: '?some=search-string',
hash: '#howdy',
state: {
[userDefined]: true
}
}
// before:
<Route path="/about" component={About}/>
<Route path="/:user" component={User}/>
<Route component={NoMatch}/>
// after:
<Route exact path="/" component={Home}/>
<Route path="/about" component={About}/>
<Route path="/:user" component={User}/>
<Route component={NoMatch}/>
Problem: Cross origin requests
React is great! But...
from jQuery to React is not easy