30
loading...
This website collects cookies to deliver better user experience
const element = <h1>Hello, world!</h1>;
function
keyword. Props are simply function arguments and can be directly used inside JSX:const Card = (props) => {
return(
<h2>Title: {props.title}</h2>
)
}
function Card(props){
return(
<h2>Title: {props.title}</h2>
)
}
class
keyword. Props need to be accessed using the this
keyword:class Card extends React.Component{
constructor(props){
super(props);
}
render(){
return(
<h2>Title: {this.props.title}</h2>
)
}
}
useState
hook to be able to handle state:const Counter = (props) => {
const [counter, setCounter] = useState(0);
const increment = () => {
setCounter(++counter);
}
return (
<div>
<p>Count: {counter}</p>
<button onClick={increment}>Increment Counter</button>
</div>
)
}
class Counter extends React.Component {
constructor(props){
super(props);
this.state = {counter : 0};
this.increment = this.increment.bind(this);
}
increment {
this.setState((prevState) => {
return {counter: prevState.counter++};
});
}
render() {
return (
<div>
<p>Count: {this.state.counter}</p>
<button onClick={this.increment}>Increment Counter</button>
</div>
)
}
}
Real DOM | Virtual DOM |
---|---|
Slow & expensive DOM manipulation | Fast & inexpensive DOM manipulation |
Allows direct updates from HTML | It cannot be used to update HTML directly |
Wastes too much memory | Less memory consumption |
componentDidMount()
: Runs after the component output has been rendered to the DOM.componentDidUpdate()
: Runs immediately after updating occurs.componentWillUnmount()
: Runs after the component is unmounted from the DOM and is used to clear up the memory space.useEffect
adds, for example, the ability to perform side effects and provides the same functionality as componentDidMount
, componentDidUpdate
, and componentWillUnmount
.React.Component
but it provides in some cases a performance boost if its render()
function renders the same result given the same props and state.const EnhancedComponent = higherOrderComponent(WrappedComponent);
this
keyword inside class components.React.createRef()
, a callback function or a string (in legacy API).