React के आम Design Patterns
In this page:
Named Patterns क्यों सीखें
Common code shapes को नाम देना (जैसे 'compound components' या 'render props') developers को design ideas जल्दी communicate करने देता है, और unfamiliar codebases में familiar structures को तेज़ी से पहचानने देता है, हर बार scratch से फिर से derive करने के बजाय कि कुछ कैसे काम करता है।
उदाहरण: Why Learn Named Patterns
<!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">
// Recognizing the shape:
function useToggle(initial) {
const [value, setValue] = React.useState(initial);
return [value, () => setValue(v => !v)];
}
// "Oh, this is the custom hook pattern for shared stateful logic"
function App() {
const [isOn] = useToggle(false);
return <p>{isOn ? "On" : "Off"}</p>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<App />);
</script>
</body>
</html>
Modern React Hooks को पुराने Patterns से ज़्यादा Favor करता है
Render props और higher-order components जैसे patterns Hooks के exist होने से पहले components के बीच logic share करने का primary तरीका थे। आज, एक custom hook आमतौर पर कम nesting और clearer code के साथ वही goal accomplish करता है, इसलिए hooks generally नए code में reach करने के लिए first tool हैं।
उदाहरण: Modern React Favors Hooks Over Older Patterns
<!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">
// Older pattern (HOC):
function withCounter(Wrapped) {
return function(props) {
const [count, setCount] = React.useState(0);
return <Wrapped {...props} count={count} increment={() => setCount(count + 1)} />;
};
}
// Modern equivalent (custom hook) is usually preferred for new code:
function useCounter() {
const [count, setCount] = React.useState(0);
return { count, increment: () => setCount(c => c + 1) };
}
ReactDOM.createRoot(document.getElementById('root')).render(<p>See dedicated pattern tutorials for full examples</p>);
</script>
</body>
</html>
Simplest Pattern चुनना जो Fit हो
हर pattern कुछ simplicity को कुछ flexibility या reusability के लिए trade करता है। एक complex pattern (जैसे compound components) को एक problem पर apply करना जिसे सिर्फ एक straightforward implementation की ज़रूरत है unnecessary abstraction जोड़ता है — इसे तब के लिए save करें जब flexibility actually ज़रूरी हो।
उदाहरण: Choosing the Simplest Pattern That Fits
<!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">
// Simple, direct — fine if this is genuinely a one-off:
function SubmitButton() {
return <button type="submit">Submit</button>;
}
// Only reach for a more elaborate pattern (compound components, render props)
// once you actually need multiple, meaningfully different variations.
ReactDOM.createRoot(document.getElementById('root')).render(<SubmitButton />);
</script>
</body>
</html>
- एक pattern (जैसे render props या HOCs) reflexively habit से out of apply करना, जब एक simpler custom hook वही काम ज़्यादा clearly कर सकता है।
- किसी और के code में एक familiar pattern न पहचानना, और इसे unnecessarily scratch से rewrite करना।
- बहुत early over-abstracting करना, एक flexible pattern build करना उस case के लिए जिसे सिर्फ एक simple, concrete implementation की ज़रूरत है।
- Common React patterns में compound components, render props, HOCs, custom hooks, और controlled/uncontrolled components शामिल हैं।
- Existing code में इन patterns को पहचानना unfamiliar codebases को समझना तेज़ करता है।
- Modern React अक्सर logic share करने के लिए render props या HOCs जैसे older patterns से custom hooks को favor करता है।
- Patterns tools हैं, rules नहीं — उस simplest वाले को उपयोग करें जो actual problem solve करे।
कोई React-version restriction नहीं — ये conventions और idioms हैं, specific APIs नहीं (जहां noted है उसके अलावा, जैसे Context React 16.3 से stable)।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Error Boundaries
- React Portals
- Modals using Portals (practical use)
- React Suspense
- Code Splitting with React.lazy
- Introduction to Server Components
- Introduction to Next.js (server-side React)
- Using React with TypeScript
- Scalable Folder Architecture
- Common React Design Patterns
- Component Documentation with Storybook
- Accessibility (a11y) in React
- i18n with react-i18next
- React Security Best Practices