Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/wellguimaraes/mobx-react-typed-injection

A better, strongly typed stores injector HOC to use with MobX and React.
https://github.com/wellguimaraes/mobx-react-typed-injection

Last synced: 12 days ago
JSON representation

A better, strongly typed stores injector HOC to use with MobX and React.

Awesome Lists containing this project

README

        

# MobX React Typed Injection

A better, fully typed injector HOC to use with MobX and React.

## !!! Deprecated !!!
This package is deprecated in favor of using MobX with hooks instead.

## Benefits

- The inject function "knows" your stores, so you'll have proper TS code completion and warnings for non-matching types
- Build time errors when trying to use props that are already injected
- No need to add one more context provider to your app root

## Install it

```
yarn add mobx-react-typed-injection
```

### Use it

1. Somewhere in your project, create a `stores.ts` file, exporting `inject` and `withFakeStores` (to be used on tests):

```typescript
import { createStateManager } from 'mobx-react-typed-injection'

import search from 'path/to/searchStore'
import movieDetails from 'path/to/movieDetailsStore'
import somethingElse from 'path/to/somethingElseStore'

export const { inject, withFakeStores } = createStateManager({
search,
movieDetails,
somethingElse,
})
```

2. Inject something from the stores into a dumb component:

```typescript jsx
import { inject } from 'path/to/stores'

const _MovieHeader = (props: { title: string; releaseYear: number; poster: string; somethingElse: string }) => {
return (


Movie Poster
{props.title}

{props.releaseYear}


)
}

export const MovieHeader = inject(({ movieDetails }) => ({
title: movieDetails.title,
releaseYear: movieDetails.releaseYear,
poster: movieDetails.posters[0],
}), _MovieHeader)
```

3. Use your injected component:

```typescript jsx
// Works fine for non injected props

// TS error for props already injected

```