1. Effect Hook
React provides a built-in hook called
In general this
Using Effect Hook we can perform actions like,
- Making API Call
- Using Scheduler methods like setInterval( )
- DOM Manipulations, etc.
Syntax:
The
1.1 Updating Browser title using Effect Hook
Using the title property provided by the DOM object we can update the Browser title
Import the
File: src/components/Scoreboard/index.js
React keeps track of the effect and executes it after the render
All the state variables and props are accessible to the Effect
If you try to pass arguments to effect and access them, you'll get undefined values
1.2 Accessing State values
If you access the new state value immediately after updating the state in Event handlers, you may not get the updated value because state updates are asynchronous
Use Effect Hook, if you want to access new state values immediately after updating the state
2. Rules of Hooks
While writing/using the hooks you should follow the below rules,
- Rule 1: Hooks should be called only at the top level
- Rule 2: Hooks should be called only inside React Function Components and Custom Hooks
React provides the eslint-plugin-react-hooks package that throws errors if we don’t follow these rules
3. Scoreboard Application Code
Use the below command to get the Final Code for Scoreboard Application