import { Component } from 'react';
// <Props, State>
class CounterState extends Component<object, { counter: number }> {
constructor(props: object) {
super(props);
this.state = {
counter: 0
};
}
increment() {
// If your state involves previous state, then you should write it as a call back function.
this.setState(
(prev) => ({
counter: prev.counter + 2
}),
() => console.log(this.state.counter)
);
}
render() {
return (
<div>
{this.state.counter}
<button onClick={() => this.increment()}> Add</button>
</div>
);
}
}
export default CounterState;
import { Component } from 'react';
// The types of Component
class Welcome extends Component<{ name: string; age: number }, object> {
render() {
// Data destructuring. If we dont have 'state', then we don't need constructor.
const { name, age } = this.props;
return (
<div>
I am {name}, I am {age} years old.
</div>
);
}
}
export default Welcome;
import React from 'react';
const heading = {
fontSize: '48px',
color: 'red'
};
const Inline: React.FC<{}> = () => {
return <div style={heading}>Inline page!</div>;
};
export default Inline;
useState
useEffect
Optimize performance
useCallback
useMemo
React.memo
State Immutability and Render
v-bind (parent-> children)
props
v-on (children->parent) / emit
ref
useRef, forwardRef
HOCs / render props
useMemo