ब्लॉग

React Native Codebase को व्यवस्थित करना

9 फ़रवरी 2023 7 मिनट पढ़ने का समय

बड़े React Native applications को व्यवस्थित करने और उनका structure बनाने के best practices। Folder structure, component organization और maintainable code patterns के बारे में जानें।

React Native Codebase को व्यवस्थित करना
React Nativeआर्किटेक्चरकोड ऑर्गनाइज़ेशनबेस्ट प्रैक्टिसेज़

अपने React Native Codebase को व्यवस्थित करना

एक scalable और maintainable codebase बनाने की गाइड

React Native application में codebase को व्यवस्थित रखना maintainability और scalability के लिए बहुत ज़रूरी है। आप अपनी files और folders को किस तरह structure करते हैं, इसका सीधा असर इस पर पड़ता है कि भविष्य में codebase के साथ काम करना कितना आसान होगा।

इस article में हम React Native application के लिए feature-based folder structure और उसके हर folder के मकसद को देखेंगे।

src Folder

src folder आपके application के पूरे source code की root directory है। इस folder के अंदर कई और subfolders होते हैं, और हर एक का अपना खास काम होता है।

Types

types folder में आप अपने application के global types define करते हैं। इसमें module types, interface definitions और कोई भी दूसरे custom types आते हैं जिनकी ज़रूरत पूरे application में पड़ती है।

सारे types के लिए एक central जगह होने से application बढ़ने के साथ उन्हें manage और maintain करना आसान हो जाता है। साथ ही, इससे यह पक्का होता है कि सभी features में types एक जैसे इस्तेमाल हों, जिससे type से जुड़े bugs का खतरा कम होता है और अगर वे आएँ तो उन्हें पकड़ना आसान होता है।

Lib

lib folder में आप application से जुड़ी वे files रख सकते हैं जो पूरे app में globally इस्तेमाल होती हैं। इसमें API calls के लिए axios instance, error handling services, constants, react-query में query client का setup वगैरह आ सकते हैं।

इन files को एक central जगह पर रखने से इन्हें maintain करना आसान होता है और यह पक्का होता है कि ये पूरे application में एक जैसी रहें।

Components

components folder में आप अपने सारे reusable components रखते हैं। ये components अलग-थलग (isolated) होने चाहिए और किसी खास feature या domain से बंधे नहीं होने चाहिए। ऐसे components के उदाहरण हैं headers, buttons और list items।

इन components को feature-specific components से अलग रखने से इन्हें पूरे application में manage और reuse करना आसान होता है, और code में duplication कम होता है।

Hooks

hooks folder में आप वे custom hooks रखते हैं जो पूरे application में इस्तेमाल होते हैं। React में hook एक खास तरह का function है जिससे आप stateful logic को अलग-अलग components में reuse कर सकते हैं।

अपने सारे **reusable/कई जगह इस्तेमाल होने वाले **custom hooks एक central जगह पर रखने से उन्हें manage और maintain करना आसान होता है, code duplication का खतरा कम होता है, और application के अलग-अलग हिस्सों में hooks share करना आसान हो जाता है।

Localization

localization folder में आप application के internationalization से जुड़ी सारी files रखते हैं। इसमें language files, string constants और कई भाषाओं को support करने के लिए ज़रूरी दूसरे resources आते हैं।

इन files को एक central जगह पर रखने से इन्हें manage और maintain करना आसान होता है, code duplication का खतरा कम होता है, और application में कई भाषाओं को support करना आसान हो जाता है।

Navigation

navigation folder में आप application के navigation से जुड़ा सारा code रखते हैं। इसमें stacks, types, routes और navigation को support करने के लिए ज़रूरी दूसरे components या functions आते हैं।

सारा navigation code एक central जगह पर होने से इसे manage और maintain करना आसान होता है, code duplication का खतरा कम होता है, और application के अलग-अलग हिस्सों में navigation functionality share करना आसान हो जाता है।

Services

services folder में आप application के underlying infrastructure से जुड़ा सारा code रखते हैं। इसमें push notifications, error monitoring और ऐसी दूसरी services का code आता है जो application की बुनियादी functionality देती हैं।

Theme

theme folder में आपको अपने app की styling रखनी चाहिए, जैसे dark/light theme। styling और theme files को एक जगह रखने से ज़रूरत पड़ने पर app के look and feel को manage और update करना आसान होता है।

Utils

Utility functions आपके React Native application का एक अहम हिस्सा हो सकते हैं, क्योंकि इनसे ऐसा code reuse किया जा सकता है जो application के कई हिस्सों में काम आता है। application का utils folder उन utility functions को रखने के लिए बढ़िया जगह है जो पूरे application में इस्तेमाल होते हैं। इन functions को एक जगह centralize करके आप पक्का कर सकते हैं कि code के दूसरे हिस्सों में इन्हें ढूँढना और इस्तेमाल करना आसान हो। उदाहरण के लिए: react-native layout से जुड़े utility functions।

Features

जब complex applications बनाने की बात आती है, तो एक साफ और सीधा folder structure होना ज़रूरी है जो सारे code को एक जगह समेट दे। features folder इसमें मदद करता है, क्योंकि यह application के अलग-अलग features को अलग-अलग categories में बाँट देता है। इससे developers किसी खास feature को जल्दी ढूँढ सकते हैं और application के दूसरे हिस्सों पर असर डाले बिना उसमें बदलाव कर सकते हैं।

इसके अलावा, जैसे-जैसे application बढ़ता है और नए features जुड़ते हैं, folder structure व्यवस्थित बना रहता है, जिससे नए developers के लिए जुड़ना और यह समझना आसान होता है कि application कैसे काम करता है। concerns का साफ बँटवारा bugs का खतरा कम करने में भी मदद करता है और application की overall performance बेहतर करता है।

एक auth folder कुछ ऐसा दिख सकता है:

    src/
    ├── ...
    └── features/
        ├── auth/
        |   ├── api/
        |   ├── helpers/
        |   ├── hooks/
        |   ├── screens/
        |   ├── services/
        |   └── store/
        └── ... (other features)

features folder की अहमियत इस बात में है कि यह codebase को छोटे, संभालने लायक हिस्सों में बाँटने में मदद करता है। हर feature के अपने subfolders होते हैं, जैसे api, helpers (या utils), hooks, screens, services और store, जो सिर्फ उसी feature के लिए होते हैं। इससे यह समझना आसान होता है कि हर feature कैसे काम करता है और वे एक-दूसरे से कैसे interact करते हैं।

api folder में authentication से जुड़ा सारा API code होता है। इससे API calls को manage करना और व्यवस्थित रखना आसान होता है।

helpers folder में वे helper methods या utility functions होते हैं जो authentication feature के लिए खास हैं। इन्हें सिर्फ authentication feature के अंदर इस्तेमाल किया जा सकता है और application के दूसरे हिस्सों के साथ share करने की ज़रूरत नहीं होती।

hooks folder में वे hooks होते हैं जो खास तौर पर authentication से जुड़े हैं। React काफी हद तक hooks पर निर्भर है, इसलिए इससे hooks को manage करना और व्यवस्थित रखना आसान होता है।

screens folder में authentication feature की सारी screens होती हैं। इसमें login screen, sign-up screen और forgotten password screen वगैरह आती हैं।

services folder में वे services होती हैं जो authentication feature के लिए खास हैं। उदाहरण के लिए, आपके पास एक authentication service हो सकती है जो user logins और sign-ups संभालती है।

आखिर में, store folder में authentication feature का state management code होता है। यह Redux store हो, Zustand store हो, या आपकी पसंद की कोई भी दूसरी state management library।

application को छोटे, व्यवस्थित हिस्सों में बाँटने से code को manage करना और ज़रूरत के हिसाब से उसमें बदलाव करना काफी आसान हो जाता है। इससे code साफ और पढ़ने लायक भी रहता है, जिससे दूसरे developers के लिए code को समझना और उस पर काम करना आसान होता है।

auth feature folder का sample structure

    src/
    └── features/
        └── auth/
            ├── api/
            |   ├── login.ts
            |   ├── signup.ts
            |   └── refresh-token.ts
            ├── helpers/
            |   ├── validateToken.ts
            |   └── dataTransformer.ts
            ├── screens/
            |   ├── Login.tsx
            |   └── Signup.tsx
            ├── services/
            |   └── validation.ts
            └── store/
                └── auth.ts

auth folder वह subfolder है जो आपके application के authentication feature के लिए है। इस folder की files authentication और authorization से जुड़ी हर चीज़ संभालती हैं, login और signup की APIs से लेकर उन UI screens तक जिनसे user interact करता है।

api folder में authentication feature की APIs होती हैं, जैसे login.ts और signup.ts files, जो user की login और signup requests के requests और responses संभालती हैं। refresh-token.ts file token को refresh करने का काम करती है।

helpers folder में वे helper functions होते हैं जो authentication process में काम आते हैं। उदाहरण के लिए, validateToken.ts file token को validate करने के लिए इस्तेमाल होती है, और dataTransformer.ts file authentication APIs में जाने और आने वाले data को transform करती है।

screens folder में वे UI components होते हैं जिनसे user interact करता है। उदाहरण के लिए, Login.tsx और Signup.tsx files में क्रमशः log in और sign up करने के UI components होते हैं।

hooks folder में authentication से जुड़े custom hooks होते हैं। उदाहरण के लिए, useLoginAction.ts और useSignupForm.ts files में ऐसे hooks हैं जो क्रमशः login और signup forms से जुड़े state और actions को manage करने में मदद करते हैं।

services folder में authentication से जुड़ी services होती हैं, जैसे validation.ts file, जिसमें user के input को validate करने वाले functions होते हैं।

store folder में authentication से जुड़ा state management होता है। auth.ts file में authentication से जुड़ा state और actions होते हैं, जिसके लिए Zustand, Redux या कोई दूसरी state management library इस्तेमाल होती है।

ध्यान दें

यह ध्यान रखना ज़रूरी है कि folders और files के naming conventions हर project और हर developer के हिसाब से अलग हो सकते हैं। हर folder का सटीक नाम नहीं, बल्कि folders का structure और organization ज़्यादा मायने रखता है। उदाहरण के लिए, app के root में मौजूद utils folder का नाम helpers या utilities भी हो सकता है, या screens की जगह pages या कुछ और नाम हो सकता है जो उस project के लिए समझ में आए। मकसद एक साफ और consistent structure रखना है जो developers को code का organization समझने में मदद करे और application को maintain और scale करना आसान बनाए।

Type-based folder structure बनाम Feature-based folder structure

project के codebase को व्यवस्थित करने का एक और काफी अपनाया जाने वाला तरीका type-based folder structure है, जिसके अपने फायदे और नुकसान हैं। अगले section में हम इन दोनों folder structures की visually तुलना करेंगे।

तुलना

    Feature-based folder structure:

    src/
    └── features/
        ├── auth/
        |   ├── api/
        |   │     ├── register.tsx
        |   │     ├── login.tsx
        |   │     ├── types.tsx
        |   │     └── helpers.tsx
        |   ├── helpers/
        |   ├── hooks/
        |   └── screens/
        |   │     ├── LoginScreen.tsx
        |   │     ├── SignupScreen.tsx
        |   │     ├── WelcomeScreen.tsx
        |   │     └── HomeScreen.tsx
        |   ├── services/
        |   └── store/
        ├── todo/
        |   └── screens/
        |   │    └── TodoListScreen.tsx
        |   ├── services/
        |   └── store/
        └── ... (other features)

    Type-based folder structure:

    src/
    ├── screens/
    |   ├── LoginScreen.tsx
    |   ├── SignupScreen.tsx
    |   ├── TodoListScreen.tsx
    |   ├── WelcomeScreen.tsx
    |   └── HomeScreen.tsx
    ├── api/
    |   └── auth/
    |       ├── register.ts
    |       ├── login.ts
    |       ├── types.ts
    |       └── helpers.ts

जैसा ऊपर दिख रहा है, type-based folder structure में screens, API calls और helpers से अलग होती हैं, जिससे codebase को समझना मुश्किल हो सकता है। ध्यान दीजिए कि type-based folder structure में हम LoginScreen.tsx और TodoListScreen.tsx जैसी आपस में असंबंधित screens को एक ही folder में रख सकते हैं। इसके अलावा, code को maintain करना भी मुश्किल हो सकता है, क्योंकि संबंधित code एक जगह group नहीं होता। वहीं feature-based folder structure में authentication से जुड़ा सारा code, यानी screens, API calls, helpers और services, एक auth folder में एक साथ group होता है। इससे codebase समझना आसान होता है, क्योंकि सारा संबंधित code एक जगह होता है। यह तरीका code का बेहतर encapsulation देता है और project को scalable और भविष्य के बदलावों के लिए flexible बनाता है।

feature-based folder structure के साथ आखिरी तस्वीर

नीचे दिखाया गया है कि dummy files के साथ feature-based folder structure आखिर में कैसा दिखेगा:

    Feature-based folder structure:

    src/
    ├── components/
    │   ├── Button.tsx
    │   └── Header.tsx
    ├── hooks/
    │   └── useHeader.ts
    ├── localization/
    │   ├── languages.ts
    │   └── locales.ts
    ├── lib/
    │   ├── axios-instance.ts
    │   └── constants.ts
    ├── navigation/
    │   ├── root-navigator.tsx
    │   └── auth-stack.tsx
    ├── services/
    │   ├── notification.ts
    │   └── logging.ts
    ├── theme/
    │   └── index.ts
    ├── types/
    │   ├── env.d.ts
    │   └── react-augment.d.ts
    ├── utils/
    │   └── layout.ts
    └── features/
        ├── auth/
        │   ├── api/
        │   │   ├── login.ts
        │   │   ├── signup.ts
        │   │   └── refresh-token.ts
        │   ├── helpers/
        │   │   ├── validateToken.ts
        │   │   └── dataTransformer.ts
        │   ├── hooks/
        │   │   ├── useLoginAction.ts
        │   │   └── useSignupForm.ts
        │   ├── screens/
        │   │   ├── Login.tsx
        │   │   └── Signup.tsx
        │   ├── services/
        │   │   └── validation.ts
        │   └── store/
        │       └── auth.ts (zustand, redux, jotai etc.)
        └── ... (other features)

ऊपर अलग-अलग folders में जोड़ी गई files (जैसे components folder में button.tsx, header.tsx, hooks folder में useHeader.ts वगैरह) सिर्फ उदाहरण के तौर पर हैं और इन्हें 100% सटीक नहीं माना जाना चाहिए। इन files को शामिल करने का मकसद folder structure की एक visual तस्वीर देना और codebase के organization को बेहतर समझने में मदद करना है। असली implementation project की खास ज़रूरतों के हिसाब से अलग हो सकता है। इस article का focus individual files की बारीकियों पर नहीं, बल्कि feature-based folder structure पर है और इस पर कि यह एक scalable और व्यवस्थित codebase बनाने में कैसे मदद कर सकता है।


निष्कर्ष

मेरी राय में, feature-based folder structure React/React Native projects को व्यवस्थित करने का एक उपयोगी तरीका है। code को अलग-अलग features में बाँटने से मुझे application को maintain करना, समझना और develop करना आसान लगता है। यह structure code का बेहतर encapsulation देता है, जिससे project scalable और भविष्य के बदलावों के लिए flexible बनता है। बेशक, यह हर किसी के लिए एक जैसा समाधान नहीं है, और सटीक structure project के size और complexity के हिसाब से बदल सकता है। लेकिन मूल बात यह है कि अलग-अलग features और उनके components, services और data के बीच साफ बँटवारा रखा जाए। मेरे अनुभव में, इस structure ने मुझे ऐसे व्यवस्थित projects बनाने और maintain करने में मदद की है जो समय के साथ बढ़त और बदलाव संभाल सकें। मैं यह तरीका उन लोगों के साथ share करना चाहता था जिन्हें यह उपयोगी लग सकता है।

यहाँ भी प्रकाशित

यह article यहाँ भी प्रकाशित है: