Hooks in React
Hooks is to manage the state in functional compenents.
there are two compenents
1)class component
2)Functional component
class component code are difficult to work with ,so they introduce functional component to make easier code. hooks helps functional component to use class components properties smoothly.
useState()
useState() is a React Hook used to create and manage state (data) inside a functional component. When the state changes, React re-renders the component and updates the UI.
Syntax:
const [state, setState] = useState(initialValue)
import React, { useState } from 'react'
function Blog() {
const [like, setLike] = useState(0)
return (
<div>
<h2>My First Blog</h2>
<h3>Likes: {like}</h3>
<button onClick={() => setLike(like + 1)}>
Like
</button>
</div>
)
}
export default Blog
Explanation:
- like β Stores the current value.
- setLike β Updates the value.
- 0 β Initial value.
- setLike(like + 1) β Increases the like count by one.
useEffect
useEffect() is a React Hook used to perform side effects in a component, such as fetching API data, updating the document title, setting subscriptions or working with timers.
Syntax:
useEffect(() => {
// Side effect code
}, [dependencies])
import React, { useEffect } from 'react'
function Blog() {
useEffect(() => {
console.log("Blog Page Loaded")
}, [])
return (
<div>
<h1>My Blog Website</h1>
</div>
)
}
export default Blog
Explanation:
- useEffect() β Executes side-effect code.
- [ ] β Runs after the component's initial mount.
Three important useEffect patterns:
useEffect(() => {}) After every render
useEffect(() => {}, []) After initial mount
useEffect(() => {}, [count]) After initial mount and when count changes
useContext()
useContext() is a React Hook used to share data between components without passing props manually through every intermediate component.
It helps avoid Prop Drilling.
Example: Share username across Blog components
First, create UserContext.jsx.
import { createContext } from 'react'
const UserContext = createContext()
export default UserContext
Next, create App.jsx.
import React from 'react'
import UserContext from './UserContext'
import Blog from './Blog'
function App() {
const username = "Viyan"
return (
<UserContext.Provider value={username}>
<Blog />
</UserContext.Provider>
)
}
export default App
Next, create Blog.jsx.
import React, { useContext } from 'react'
import UserContext from './UserContext'
function Blog() {
const username = useContext(UserContext)
return (
<div>
<h1>Welcome {username}</h1>
</div>
)
}
export default Blog
Explanation:
- createContext() β Creates a context.
- UserContext.Provider β Shares data with child components.
- value={username} β Provides the data.
- useContext(UserContext) β Receives the shared data.
useNavigate()
useNavigate() is a React Router Hook used to navigate from one page to another programmatically without reloading the entire website.
First install React Router:
npm install react-router-dom
Example: Navigate from Home to Blog page
import React from 'react'
import {
BrowserRouter,
Routes,
Route,
useNavigate
} from 'react-router-dom'
function Home() {
const navigate = useNavigate()
return (
<div>
<h1>Home Page</h1>
<button onClick={() => navigate('/blog')}>
Go to Blog
</button>
</div>
)
}
function Blog() {
return (
<h1>Welcome to Blog Page</h1>
)
}
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/blog" element={<Blog />} />
</Routes>
</BrowserRouter>
)
}
export default App
Explanation:
const navigate = useNavigate()
Creates a navigation function.
navigate('/blog')
Navigates to the Blog page.
Other useful examples:
navigate('/') // Go to Home
navigate('/about') // Go to About
useRef()
useRef() is a React Hook used to store a value that persists between renders without causing a re-render when that value changes. It can also be used to access DOM elements directly.
Syntax:
const myRef = useRef(initialValue)
import React, { useRef } from 'react'
function Blog() {
const inputRef = useRef(null)
function focusInput() {
inputRef.current.focus()
}
return (
<div>
<h2>Search Blog</h2>
<input
ref={inputRef}
type="text"
placeholder="Enter blog name"
/>
<button onClick={focusInput}>
Focus Input
</button>
</div>
)
}
export default Blog
Explanation:
- useRef(null) β Creates a reference.
- ref={inputRef} β Connects the reference to the input element.
- inputRef.current β Accesses the actual input element.
- .focus() β Focuses the input field.
BrowserRouter
BrowserRouter is a React Router component used to enable client-side navigation in a React application using the browser's URL.
It allows users to move between different pages without completely reloading the website.
import React from 'react'
import {
BrowserRouter,
Routes,
Route,
Link
} from 'react-router-dom'
function Home() {
return <h1>Home Page</h1>
}
function About() {
return <h1>About Page</h1>
}
function Blog() {
return <h1>Blog Page</h1>
}
function App() {
return (
<BrowserRouter>
<nav>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
<Link to="/blog">Blog</Link>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/blog" element={<Blog />} />
</Routes>
</BrowserRouter>
)
}
export default App
Explanation:
Component-Purpose
BrowserRouter-Enables routing
Routes-Contains route definitions
Route-Defines URL and component
Link-Navigates between pages
Top comments (0)