Rishit Week 6
Rishit Week 6
Rishit Week 6
React Router is a library for handling navigation in React applications. It enables you to create a
single-page web application with navigation without the need for a full page refresh. React Router
provides a set of components that allow you to define how your application's UI should change in
response to a change in the URL.
1.BrowserRouter: This component uses the HTML5 history API to keep UI in sync with the URL.
2.Route: It is used to render content based on the URL. You define a route with a path and a
component that should be rendered when the path matches.
jsx code
// Example
Link: This component is used to create links to different routes in your application. It prevents a full
page reload when the link is clicked and updates the URL using client-side routing.
jsx code
// Example
Switch: It renders the first Route or Redirect that matches the current location. This is useful when
you want to render only the first matching route.
jsx code
// Example
</Switch>
Redirect: It renders a redirect to another route. This is useful for programmatically navigating to
another route.
Jsx code
import { Redirect } from 'react-router-dom';
// Example
React Router provides a flexible and declarative way to handle navigation in your React applications,
making it easier to create complex single-page applications with dynamic UI updates based on the
URL. It's widely used in the React ecosystem for building modern web applications.
React Hooks are functions that allow functional components in React to manage state and side
effects. Before the introduction of hooks, state and lifecycle methods were primarily handled in class
components. However, hooks provide a more concise and functional way to achieve the same, and
they were introduced in React version 16.8.
1.useState:
Purpose: Manages local state in functional components.
Example:
function ExampleComponent() {
return (
<div>
</div> );
2.useEffect:
Purpose: Performs side effects in functional components. It replaces lifecycle methods like
componentDidMount, componentDidUpdate, and componentWillUnmount.
Example:
function ExampleComponent() {
useEffect(() => {
}, []);
// Empty dependency array means this effect runs once after the initial render return (
<ul>
{data.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>
);
useContext:
Purpose: Allows you to subscribe to React context without introducing nesting. It lets you consume
the value of a context within a function component.
Example:
useReducer:
Purpose: A more advanced alternative to useState. It is typically used when the next state depends
on the previous one and when state logic is complex.
Example:
jsxCopy code
import React, { useReducer } from 'react'; const initialState = { count: 0 }; function reducer(state,
action) { switch (action.type) { case 'increment': return { count: state.count + 1 }; case 'decrement':
return { count: state.count - 1 }; default: throw new Error(); } } function Counter() { const [state,
dispatch] = useReducer(reducer, initialState); return ( <div> Count: {state.count} <button onClick={()
=> dispatch({ type: 'increment' })}>+</button> <button onClick={() => dispatch({ type:
'decrement' })}>-</button> </div> ); }
These hooks enable developers to write more modular and readable code, making it easier to
manage state and side effects in functional components. They have become a standard and powerful
way to build React applications.