function MyComponent() {
return (
<>
<h1>My Title</h1>
<p>.....</p>
</>
);
}
Well, it is...!!
class MyApp extends React.Component {
// Function we can pass straight in
// or one which gets called by react
handler = (event) => {
event.persist();
console.log(event.target.value);
// .....
};
// A function we need to pass args to
// when we call it
log = (num) => {
console.log(num)
};
render() {
console.log('rendering');
return (
<>
<h1>Search</h1>
<input onChange={this.handler} />
<button onClick={() => {
this.log(2)
}}>Click me</button>
</>
);
}
}
ReactDOM.render(<MyApp />, document.getElementById('mount'));
But how do we set up that data; make intelligent decisions about it, or; decide what's drawn?
So we can interrupt and supervise the process...
The bits in the blue boxes are method names you can use to override the default behaviour and put your own custom code in.
A place to:
class TodoApp extends React.Component {
constructor(props) {
super(props);
this.state = {items: [], text: ''};
this.someMethod = this.someMethod.bind(this);
}
// ...
}
class TodoApp extends React.Component {
constructor(props) {
super(props);
this.state = { mode: null } ;
}
componentWillMount() {
let mode;
if (this.props.age > 70) {
mode = 'old';
} else if (this.props.age < 18) {
mode = 'young';
} else {
mode = 'middle';
}
this.setState({ mode });
}
// ... after CWM/CDM and before render
someMethod() {
doSomething();
}
}
class TodoApp extends React.Component {
constructor(props) {
super(props);
this.state = {items: [], text: ''};
this.someMethod = this.someMethod.bind(this);
}
// ...
someMethod() {
doSomething();
}
render() {
// Fragments can be expressed as <>....</> aswell
return (
<>
<h1 className="main-title">{this.props.title}</h1>
<button onClick={this.someMethod}>Do something</button>
</>
);
}
}
class TodoApp extends React.Component {
constructor(props) {
super(props);
this.state = {items: [], text: '', cars: []};
this.someMethod = this.someMethod.bind(this);
}
// ...
componentDidMount() {
// Axios is an AJAX client. ('fetch' is another, but isomorphic!)
axios.get('/cars')
.then((resp) => {
this.setState({cars: resp});
})
.catch((err) => {
console.log('Error', err.message);
});
}
// ...
}
class TodoApp extends React.Component {
constructor(props) {
super(props);
this.state = { suspended: false };
this.someMethod = this.someMethod.bind(this);
}
// ...
componentWillReceiveProps(newProps, newContext) {
if (newProps.suspended) {
this.setState({ suspended: true });
}
}
}
class TodoApp extends React.Component {
constructor(props) {
super(props);
this.state = {items: [], text: ''};
this.someMethod = this.someMethod.bind(this);
}
shouldComponentUpdate(newProps, newState) {
if (newProps.suspended) {
return false;
}
return true;
}
// ...
}
class TodoApp extends React.Component {
constructor(props) {
super(props);
this.state = {items: [], text: ''};
this.someMethod = this.someMethod.bind(this);
}
componentWillUpdate(newProps, newState, newContext) {
if (newState.low) {
newProps.units +=1;
}
}
// ...
}
class TodoApp extends React.Component {
constructor(props) {
super(props);
this.state = {items: [], text: ''};
this.someMethod = this.someMethod.bind(this);
}
componentDidUpdate(prevProps, prevState) {
// only update chart if the data has changed
if (prevProps.data !== this.props.data) {
this.chart = c3.load({
data: this.props.data
});
}
}
// ...
}
class TodoApp extends React.Component {
constructor(props) {
super(props);
this.state = { items: [], text: '', interval: null };
this.someMethod = this.someMethod.bind(this);
}
componentDidMount() {
const timer = setInterval(function(){...}, 3000);
this.setState({ interval: timer });
}
// ...
componentWillUnmount() {
clearInterval(this.state.interval);
}
}
(You already have it!)
class MyComponent extends React.Component {
render() {
const headerStyles = {
backgroundColor: 'red',
zIndex: 99999
};
return (
<h1 style={headerStyles}>{this.props.heading}</h1>
);
}
}
// OR
function MyComponent(props){
return (
<h1 style={{
color: '#f00'
}}>{props.head}</h1>
);
}
.container {
max-width: 1280px;
margin: auto;
}
import { container } from './styles.css';
function MyComponent(){
return (
<h1 className={container}>Hello!</h1>
);
}
styles.css
MyComponent.js
Often used with the classnames package for multiple, dynamic classes