React uses a ton of ES6 features
Let's go through the vital ones
let foo = 'foo!';
const BAR = 'BAR!';
Overly simplified: let is an other way to declare a variable and const means this variable's value can't be changed.
const noParam = () => {
return true
}
const oneParam = (a) => {
return a * 2
}
const oneParam = a => {
return a * 2
}
const twoParams = (a, b) => {
return a * b
}
Again, very simplified: a different syntax to declare functions. Allows one-liners.
function noParam() {
return true
}
function oneParam(a) {
return a * 2
}
function twoParams(a, b) {
return a * b
}
const noParam = () => true
const oneParam = (a) => a * 2
const oneParam = a => a * 2
const twoParams = (a, b) => a * b
Allows one-liners.
import * as React from 'react'
import * as ReactDOM from 'react-dom'
ReactDOM.render(
<FilterableProductTable>
<SearchBar />
<ProductTable>
<ProductCategory>Sporting Goods</ProductCategory>
<ProductRow name='Football' price='49.99' />
<ProductRow name='Baseball' price='9.99' />
<ProductRow name='Basketball' price='29.99' outOfStock />
</ProductTable>
</FilterableProductTable>
, document.getElementByID('react-root'))
import * as React from 'react'
import * as ReactDOM from 'react-dom'
class HelloMessage extends React.Component {
render() {
return <div>Hello {this.props.name}</div>;
}
}
ReactDOM.render(<HelloMessage name="John" />, mountNode);
// JS
render() {
const label = 'Submit'
const iconClass = 'user-icon'
return (
<h1>Foobar</h1>
<IconButton>
<Icon class={iconClass} />
<Label>{label}</Label>
</IconButton>
)
}
<!-- HTML -->
<h1>Foobar</h1>
<button>
<i class="user-icon"></i>
Submit
</button>
React has a very small, but efficient, API making it easy to learn.
In fact, let's go through all of it.
The internal state (duh) of a component
class Button extends React.Component {
constructor(props) {
super(props)
this.state = { label: 'Foo' }
}
render() {
return (
<button onClick={() => this.setState({label: 'bar'})}>
{this.state.label}
</button>
)
}
}
Defined on instantiation, passed as attributes in JSX
class Hello extends React.Component {
render() {
return (
<p>Hello {this.props.name}</p>
)
}
}
ReactDOM.render(<Hello name="Guyllaume" />, rootNode);
Pretty easy, right?