1. React Components
There are two ways to write React Components.
- Function Components
- Class Components
Feature | Function Components | Class Components |
---|---|---|
State | No | Yes |
Life Cycle Methods | No | Yes |
Props | Yes | Yes |
2. React Hooks
React Hooks are special functions that are a new addition to React
They allow us to use state and other React features(lifecycle methods, context, etc) in the Function Components
2.1 Importance of React Hooks
- Easy to reuse logic between the components
- Components become simple and easy to understand
- Less lines of code
- No need of switching between Class & Function Components
- Developers and Companies are gradually adapting to React Hooks
2.2 Hooks for Various Purposes
React provides multiple hooks which can be used for different purposes
Hook | Purpose |
---|---|
useState() | Add State to Function Components |
useEffect() | Execute Logic after the Component render |
useContext() | Access the Context value |
3. State Hook
State Hook is a built-in Hook that allows us to add state to the Function Components
Syntax:
useState accepts the initial value as an argument and returns an array with two values: the current state and a function to update it
In the Class Components, the state is an Object. Where in the Function Components, the state can be of any data type i.e, boolean, number, string, object, etc.
3.1 Adding State
For adding the state to Function Components, import the
File: src/components/GreetUser/index.js
In the above code, we provided an empty string as the initial value to
3.2 Updating State
We can update the state using setter function by passing value/callback function as an argument
Using Value: Update the state using value if the next state is independent of the previous state
Using Callback function: Update the state using callback function if the next state is computed based on the previous state
3.2.1 Updating State using Value
File: src/components/GreetUser/index.js
Class Component:
- When the state updates, the component re-renders only the render method will be called
Function Component:
- When the state updates, the entire Component will be re-rendered
- Even if the entire component is re-rendered, React will preserve the state variables between the re-renders
4. Global Styling
Sometimes, we may need to apply the styles globally to our application
For example:
- Applying a font family for all the text in our application
- Overriding some browser default styling
4.1 Creating Global Styles with Styled Components
- We write global styles in the styledComponents.jsfile. Inside theGlobalStylevariable we define all global styles.
- We will import GlobalStylecomponent and place it at the top of React tree. In many react applications, that’s typically theApp.jsfile.
Example:
File: src/styledComponents.js
File: src/App.js
- To know more, you can go through this link.