This repository showcases practical examples of React hooks, demonstrating their functionality and usage in functional components. Each file focuses on a specific hook, providing clear and concise examples to help developers understand and implement React hooks effectively.
React hooks are special functions that allow you to use state, manage side effects, and leverage other React features in functional components. They eliminate the need for class components, making code simpler and more reusable.
The useState
hook enables you to add and manage state in a functional component. It returns a state variable and a function to update it.
A counter that increments, decrements, or resets based on user interaction.
The useRef
hook creates a mutable reference that persists across renders without triggering re-renders. It’s commonly used to access or manipulate DOM elements.
Focusing an input field programmatically when a button is clicked.
The useReducer
hook manages complex state logic, similar to Redux. It’s ideal for handling multiple state transitions or related state values.
A counter combined with a toggle to show or hide text.
The useEffect
hook handles side effects, such as fetching data, updating the DOM, or subscribing to events, after a component renders.
A placeholder for implementing side effect logic (e.g., fetching data on component mount).
The useLayoutEffect
hook is similar to useEffect
but runs synchronously before the browser paints, making it suitable for DOM measurements or mutations.
Comparing useLayoutEffect
with useEffect
to demonstrate rendering differences.
Hooks-in-React-js-main/
├── useState_hook.jsx # Demonstrates useState with a counter
├── useRef_Hook.jsx # Demonstrates useRef for DOM manipulation
├── useRender_hook.jsx # Demonstrates useReducer for complex state
├── useEffect_hook.jsx # Placeholder for useEffect examples
├── useLayoutEffect_hook.jsx # Demonstrates useLayoutEffect vs useEffect
├── README.md # Project documentation
-
Clone the repository:
git clone <repository-url>
-
Navigate to the project directory:
cd Hooks-in-React-js-main
-
Install dependencies:
npm install
-
Start the development server:
npm start
This will launch the project in your default browser, allowing you to interact with the hook examples.
This project is a coursework submission and is not bound by a specific license.
Created by D. Naga Phanindra.