What is React JS?
React JS is a JavaScript library for building user interfaces.
It was developed by Meta (Facebook).
React allows us to break a large user interface into small, reusable components.
A component is usually a JavaScript function that returns UI using JSX.
Installing React JS
Modern React projects are commonly created using tools such as Vite.
Create a React project using:
Then install the dependencies:
Start the development server:
Creating a React Component
A React component can be created using a JavaScript function.
A component returns JSX that describes what should appear on the screen.
Importing and Exporting Components
A component can be exported from one file and imported into another.
Export
Import
The imported component can then be used:
JSX
JSX is a syntax extension for JavaScript that allows us to write HTML-like syntax inside JavaScript.
Example:
Important JSX Rules
- Return a single parent element.
- Close all HTML tags properly.
- Use
classNameinstead ofclass. - Use curly brackets
{}to write JavaScript expressions.
Example:
React Fragment
A Fragment allows us to group multiple elements without adding an extra HTML element.
<>...</> is called a React Fragment.
JavaScript in JSX
Use {} to insert JavaScript expressions into JSX.
You can also use expressions:
Output:
Styling in React
Inline styles are written using an object.
The first {} tells JSX that JavaScript is being used.
The second {} represents the JavaScript object containing the styles.
Props
Props are used to pass data from a parent component to a child component.
Pass props:
Output:
Props are read-only.
Conditional Rendering
Conditional rendering means displaying different UI based on a condition.
Rendering Lists
The map() method is commonly used to display a list.
The key helps React identify list items.
Event Handling
React can respond to events such as clicks.
Common events include:
Hooks
Hooks are functions that allow React components to use features such as state and side effects.
Common hooks include:
useState()useEffect()useReducer()useContext()useCallback()useMemo()useRef()
useState()
useState() is used to create and update state.
useState() returns two values:
When the state changes, React re-renders the component.
useEffect()
useEffect() is used to perform side effects in a component.
Examples of side effects include:
- Fetching data
- Timers
- Subscribing to events
- Updating external systems
Import:
Run After Every Render
Without a dependency array, the effect runs after every completed render.
Run Once
An empty dependency array makes the effect run after the initial render.
Run When a Value Changes
The effect runs when user changes.
useReducer()
useReducer() is useful for managing more complex state logic.
It works with:
Basic example:
Here:
useContext()
useContext() allows components to access shared data without passing props through every level.
Example:
It is useful for shared data such as:
- User information
- Theme
- Language
- Application settings
useCallback()
useCallback() returns a memoized function.
It can help when passing functions to optimized child components.
The function is recreated when its dependencies change.
useMemo()
useMemo() returns a memoized value.
It can be useful when a calculation is expensive.
The calculation is performed again when price or quantity changes.
useRef()
useRef() is commonly used to store a value that should persist between renders or to access a DOM element directly.
Example: