Types of states
Below are the different types of states that can exist within an application. There are three main types:
1. Local State
Local state pertains to a single component. An example of this would be listening to user input in a field or toggling a “show more details” button. Typically, local state is managed within the component using useState or useReducer.
2. Cross-Component State
Cross-component state impacts multiple components. For instance, managing the open/closed state of a modal overlay. While this can also be managed using useState or useReducer, it often involves passing state between components via props drilling.
3. App-Wide State
App-wide state influences the entire application, such as themes or user authentication status. Similar to cross-component state, this can be managed using useState or useReducer, albeit with the assistance of props drilling.
How to deploy React app in Firebase
Understanding React custom Hooks
Understanding React Context
Set up a React project with Vite along with Tailwind CSS
Here is the step by step guide to create a React project with Vite.
- Run the command
npm create vitein the command prompt. - Afer the script you will be prompted to enter a project name.
- After entering the project name, Vite will prompt you to select a framework. Select the framework as React.
- After selecting framework, Vite will prompt you to select language type.
- After selecting the language type, Vite will show the message as Scaffolding the project in the location.
- After that you can go to the folder and instruct to install the dependencies with the command
npm install.
And here is the step to install Tailwind CSS as a Vite plugin.
Run the command
npm install tailwindcss @tailwindcss/viteto Installtailwindcssand@tailwindcss/vitevia npm.Add the
@tailwindcss/viteplugin to your Vite configuration.import { defineConfig } from 'vite' import tailwindcss from '@tailwindcss/vite' export default defineConfig({ plugins: [ tailwindcss(), ], })Add an
@import "tailwindcss";to your CSS file that imports Tailwind CSS.After that you can run the project with command
npm run devor whatever command is configured in yourpackage.jsonfile..
References
What is React?
Difference between React and ReactDOM
React and ReactDOM are two distinct libraries that are commonly used together in the development of web applications with React.
React is a JavaScript library focused on building user interfaces. It follows a declarative approach, where developers define what the UI should look like based on the current state of the application. React then efficiently updates and renders components as the state changes. Its design emphasizes flexibility and efficiency, making it a popular choice for developing both web and mobile applications.
ReactDOM, on the other hand, serves as a bridge between React and the DOM (Document Object Model). The DOM is a hierarchical structure representing the HTML content of a web page. ReactDOM provides a set of methods that enable React components to be rendered and updated within the DOM. It ensures that the user interface is synchronized with the underlying HTML structure.
In summary, React is a library for building user interfaces, while ReactDOM handles the interaction with the DOM, rendering React components onto the web page. Although they are often used together, they serve different purposes and can function independently of each other.
Conditional Rendering
Your components will often need to display different things depending on different conditions. In React, you can conditionally render JSX using JavaScript syntax like if statements, &&, and ? : operators.
&& operator
{ isYes && <A />}
? : operator
isYes ? Yes() : No()
- In React, you control branching logic with JavaScript.
- You can return a JSX expression conditionally with an if statement.
- You can conditionally save some JSX to a variable and then include it inside other JSX by - using the curly braces.
- In JSX,
{cond ? <A /> : <B />}means “if cond, render<A />, otherwise<B />. - In JSX,
{cond && <A />}means “if cond, render<A />, otherwise nothing”. - The shortcuts are common, but you don’t have to use them if you prefer plain if.