1. Persisting data while using React Hooks
1.1 Storing values in Local Storage
In
File: src/components/BmiCalculator/index.js
1.2 Getting values from the Local Storage
Once after getting the values from the Local Storage, provide them as the initial values for the
File: src/components/BmiCalculator/index.js
2. Tips for using the Effect Hook
2.1 Using Multiple useEffects to Separate Concerns
Just like useState, you can also use multiple useEffects to separate unrelated logic into different effects based on what it is doing
File: src/components/BmiCalculator/index.js
2.2 Optimizing Performance by Skipping Effects
In some cases, executing the effect after every render creates a performance problem, It can be optimized by Skipping the effect when it is not required
The
Syntax:
- The values we pass to the dependency array are called dependencies, which can be state variables or props
- Effect will be executed whenever the dependencies are changed
Without Dependencies
The Effect executes after every render
Passing Empty Dependency Array
The Effect executes only once after the first render
Passing Dependencies
The Effect executes when dependencies are changed
File: src/components/BmiCalculator/index.js
3. BmiCalculator Application Code
Use the below command to get the Final Code for BmiCalculator Application