November 2019
Beginner
804 pages
20h 1m
English
This hook is the functional counterpart to the React lifecycle methods of class components. The useEffect hook enables Functional Components to define logic that should execute after the component has been rendered/re-rendered.
Before we explain further, let's look at an example:
import React, {useState, useEffect} from 'react';
export function Switch(props) {
const [currentSwitchStatus, switchStatus] = useState(false);
useEffect(() => {
alert('The switch has been activated. Hopefully, this was not by mistake :)');
});
return (
<div>
<span>The switch is currently: {currentSwitchStatus? 'ON': 'OFF'} </span>
<button onClick={() => switchStatus(!currentSwitchStatus)}>Change the value! </button> ...Read now
Unlock full access