For core concepts see [[React Core Concepts]]
A refresher for intermediate React engineers.
class App extends React.Component {
render() {
return (
<div className="catch-of-the-day">
<div className="menu">
{/* tagline is the prop */}
<Header tagline="Fresh Seafood Market"/>
</div>
<Order />
<Inventory />
</div>
)
}
}
const Header = props => (
<header className="top">
<h1>
Catch
<span className="ofThe">
<span className="of">Of</span>
<span className="the">The</span>
</span>
Day
</h1>
<h3 className="tagline">
{/* here we are passing the data/props into the Header component */}
<span>{props.tagline}</span>
</h3>
</header>
);
// Presentational component
const Header = props => (
<header className="top">
<h1>
Catch
<span className="ofThe">
<span className="of">Of</span>
<span className="the">The</span>
</span>
Day
</h1>
<h3 className="tagline">
{/* functions have no `this` */}
<span>{props.tagline}</span>
</h3>
</header>
);
export default
and named exportexport function rando(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
import { rando } from '../helpers.js
import React from 'react'
defaultValue={rando()}
handleSubmit(e) {
e.preventDefault();
console.log('Going to store')
}
<button onClick={this.handleSubmit}>Click Me</button>
handleSubmit
would run the method when the component mounts (page load)ref
or syncing stuff from stateclass StorePicker extends React.Component {
myInput = React.createRef();
handleSubmit(e) {
e.preventDefault();
console.log('Going to store')
}
render(){
return (
<form className="some-class" onSubmit={this.handleSubmit}>
<h2>Please enter a store</h2>
<input
type="text"
ref={this.myInput}
required
placeholder="Store Name"
defaultValue={rando()}
>
</form>
)
}
}
this
in a React components refers to that exact componentReact.Component
componentDidMount()
or use this.handleSubmit.bind()
class StorePicker extends React.Component {
constructor() {
super();
{/* will bind `this` to this exact component */}
this.handleSubmit.bind(this);
}
handleSubmit(e) {
e.preventDefault();
console.log('Going to store')
}
render(){
return (
<form className="some-class" onSubmit={this.handleSubmit}>
<h2>Please enter a store</h2>
<input
type="text"
ref={this.myInput}
required
placeholder="Store Name"
defaultValue={rando()}
>
</form>
)
}
}
this
inside a method use bind(this)
this.myInput.value.value
to grab eventshttps://news.ycombinator.com/item?id=31088065
Have anything to add? You can reach out to me by email or You can find more of my stuff on my links page.