Skip to content

Commencement-Technology/ReactNativeDI

 
 

Repository files navigation

Sure! Below is a sample README file for your project that explains how to implement InversifyJS for dependency injection in a React Native project, along with detailed instructions on how to set it up. You can modify it as needed to suit your project.


React Native Dependency Injection with InversifyJS

This project demonstrates how to set up InversifyJS for dependency injection in a React Native application, using functional components and React hooks.

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Installation
  4. Project Structure
  5. Setting Up InversifyJS
  6. Usage in Functional Components
  7. Running the App
  8. Configuration
  9. Contributing
  10. License

Introduction

This project showcases how to use InversifyJS for dependency injection in a React Native project. InversifyJS is a powerful inversion of control (IoC) container that simplifies dependency management and enhances modularity, testability, and maintainability.

Prerequisites

Before getting started, ensure you have the following installed:

  • Node.js (version 14.x or later)
  • npm or yarn
  • React Native CLI
  • Git (optional for version control)

Installation

To set up the project, follow these steps:

  1. Clone the repository or create a new React Native project:

    npx react-native init MyInversifyApp
    cd MyInversifyApp
  2. Install the required dependencies:

    npm install inversify inversify-hooks reflect-metadata
  3. Install the Babel plugins for decorators and metadata support:

    npm install --save-dev @babel/plugin-proposal-decorators babel-plugin-transform-typescript-metadata
  4. Install TypeScript (if not installed already):

    npm install --save-dev typescript

Project Structure

Here is a simplified project structure showing where the InversifyJS setup is located:

├── src
│   ├── services
│   │   ├── GreetingService.ts
│   │   └── IGreetingService.ts
│   ├── types.ts
│   ├── inversify.config.ts
│   └── HomeScreen.tsx
├── App.tsx
├── tsconfig.json
├── babel.config.js
├── .gitignore
└── package.json
  • src/services/: Contains service interfaces and implementations.
  • src/inversify.config.ts: Sets up the InversifyJS container.
  • src/HomeScreen.tsx: Example functional component using dependency injection.

Setting Up InversifyJS

Step 1: Configure Babel for Decorators

In your babel.config.js, add the necessary plugins for supporting decorators and metadata emission:

module.exports = {
  presets: ['module:@react-native/babel-preset'],
  plugins: [
    ['@babel/plugin-proposal-decorators', { legacy: true }],
    'babel-plugin-transform-typescript-metadata',
  ],
};

Step 2: Enable Decorators and Metadata in TypeScript

In your tsconfig.json, ensure that decorators and metadata emission are enabled:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    // other options...
  }
}

Step 3: Define Types for Dependency Injection

Create a src/types.ts file to define symbols for the services:

// src/types.ts
export const TYPES = {
  IGreetingService: Symbol.for('IGreetingService'),
};

Step 4: Create Service Interface and Implementation

Define an interface and its implementation for the service you want to inject. For example, a simple greeting service.

Interface (IGreetingService.ts):

// src/services/IGreetingService.ts
export interface IGreetingService {
  getGreeting(): string;
}

Implementation (GreetingService.ts):

// src/services/GreetingService.ts
import { injectable } from 'inversify';
import { IGreetingService } from './IGreetingService';

@injectable()
export class GreetingService implements IGreetingService {
  getGreeting(): string {
    return 'Hello from InversifyJS!';
  }
}

Step 5: Set Up the InversifyJS Container

Configure the InversifyJS container to bind the interface to the implementation.

Container Setup (inversify.config.ts):

// src/inversify.config.ts
import { Container } from 'inversify';
import { IGreetingService } from './services/IGreetingService';
import { GreetingService } from './services/GreetingService';
import { TYPES } from './types';

const container = new Container();

container.bind<IGreetingService>(TYPES.IGreetingService).to(GreetingService);

export { container };

Usage in Functional Components

In a functional component, you can inject services using the useInjection hook provided by inversify-hooks.

HomeScreen (HomeScreen.tsx):

// src/HomeScreen.tsx
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import { useInjection } from 'inversify-hooks';
import { TYPES } from './types';
import { IGreetingService } from './services/IGreetingService';

const HomeScreen: React.FC = () => {
  const greetingService = useInjection<IGreetingService>(TYPES.IGreetingService);

  return (
    <View style={styles.container}>
      <Text style={styles.text}>{greetingService.getGreeting()}</Text>
    </View>
  );
};

export default HomeScreen;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  text: {
    fontSize: 18,
  },
});

Step 6: Integrate the InversifyJS Container with the App

In your App.tsx, wrap your components with the Provider from inversify-hooks and pass the container.

// App.tsx
import React from 'react';
import { Provider } from 'inversify-hooks';
import { container } from './src/inversify.config';
import HomeScreen from './src/HomeScreen';

const App: React.FC = () => (
  <Provider container={container}>
    <HomeScreen />
  </Provider>
);

export default App;

Running the App

Once the setup is complete, you can run the app using the following commands:

For Android:

npx react-native run-android

For iOS:

npx react-native run-ios

Ensure that you restart the Metro bundler with cache clearing to apply changes:

npx react-native start --reset-cache

Configuration

.gitignore

To avoid committing unnecessary files, add the following to your .gitignore file:

# Node.js dependencies
node_modules/

# Logs
logs/
*.log

# React Native specific
ios/Pods/
android/app/build/
ios/build/

# Temporary files
*.tmp
*.temp
.temp/
.tmp/

.editorconfig

If your team is using VS Code or other text editors, you can add an .editorconfig file to ensure consistent code formatting.

root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true

Contributing

Contributions are welcome! If you'd like to contribute to this project, please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature (git checkout -b feature/my-feature).
  3. Commit your changes (git commit -m 'Add my feature').
  4. Push the branch (git push origin feature/my-feature).
  5. Open a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Need Help?

If you encounter any issues while setting up InversifyJS in your React Native project, feel free to open an issue or reach out for assistance.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • TypeScript 25.3%
  • Kotlin 24.1%
  • Objective-C 22.6%
  • Ruby 11.1%
  • JavaScript 9.0%
  • Objective-C++ 7.9%