useState का परिचय
In this page:
import { useState } from "react";
const [state, setState] = useState(initialValue);
setState(newValue); // updates state and re-renders
useState से State बनाना
useState(initialValue) दो items वाला एक array return करता है: current state value, और इसे update करने के लिए एक function। Convention से, आप इन्हें array destructuring उपयोग करके दो variables में destructure करते हैं, typically value और setValue नाम दिए जाते हैं।
set नाम दें, जैसे count/setCount, consistency के लिए।उदाहरण: Creating State with useState
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function Counter() {
const [count, setCount] = React.useState(0);
return <p>Count: {count}</p>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<Counter />);
</script>
</body>
</html>
एक Re-render Trigger करने के लिए State Update करना
Setter function call करना state value update करता है AND React को नई value के साथ component को re-render करने के लिए बताता है। यही है जो UI को user actions जैसे clicks के response में actually change होने देता है।
count = count + 1) एक re-render trigger नहीं करता और UI update नहीं होगा।उदाहरण: Updating State to Trigger a Re-render
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function Counter() {
const [count, setCount] = React.useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
ReactDOM.createRoot(document.getElementById('root')).render(<Counter />);
</script>
</body>
</html>
हर Component का अपना State होता है
जब आप एक ही component को कई बार use करते हैं, हर instance को अपना independent state मिलता है -- एक को change करना दूसरों को affect नहीं करता। ऐसा है क्योंकि useState हर individual rendered component instance से tied है, globally shared नहीं।
उदाहरण: Each Component Has Its Own State
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function Counter() {
const [count, setCount] = React.useState(0);
return <button onClick={() => setCount(count + 1)}>Clicked {count} times</button>;
}
function App() {
return <div><Counter /><Counter /></div>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<App />);
</script>
</body>
</html>
- component के top level के बजाय एक loop, condition, या nested function के अंदर useState call करना।
- setter function call करने के बजाय directly state variable को mutate करना।
- same function call के अंदर setter call करने के बाद immediately state variable के update हो जाने की उम्मीद करना।
- useState एक Hook है जो एक function component में state जोड़ता है।
- यह एक pair return करता है: current value और इसे update करने के लिए एक function।
- Setter call करना नई value के साथ एक re-render trigger करता है।
- State updates same function call के अंदर synchronously apply नहीं होते।
React 16.8 से available, जब Hooks introduce हुए थे।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: