惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recorded Future
Recorded Future
Microsoft Security Blog
Microsoft Security Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
有赞技术团队
有赞技术团队
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
B
Blog RSS Feed
小众软件
小众软件
博客园 - 叶小钗
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 聂微东
博客园_首页
B
Blog
雷峰网
雷峰网
S
SegmentFault 最新的问题
N
Netflix TechBlog - Medium
D
Docker
博客园 - 司徒正美
博客园 - 【当耐特】
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
MongoDB | Blog
MongoDB | Blog
U
Unit 42
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
腾讯CDC
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
Jina AI
Jina AI
WordPress大学
WordPress大学
D
DataBreaches.Net
V
V2EX
V
Visual Studio Blog
Know Your Adversary
Know Your Adversary
P
Privacy & Cybersecurity Law Blog
F
Full Disclosure
G
Google Developers Blog
Engineering at Meta
Engineering at Meta
The Hacker News
The Hacker News
Security Archives - TechRepublic
Security Archives - TechRepublic
IT之家
IT之家
P
Privacy International News Feed

CodeBlocQ

Jest - Mock Local Storage Loose assertions on arguments passed to function with Jest TypeScript Abstract Class Check if a Docker image exists locally A-Star Pathfinding React Demo My Free and Open Source Expense Tracker App is on the App Store Pass artifacts around in between stages in gitlab CI How to start a tech company as a non technical individual Setup gitment on your Hexo blog
Have Mobx and React work with TypeScript
Jonathan Klughertz · 2020-10-05 · via CodeBlocQ

The problem with Mobx and TypeScript

The official solution to support TypeScript sucks. They are basically asking you to make stores optional in your Prop types:

class MyComponent extends React.Component<{ userStore?: IUserStore; otherProp: number }, {}> {

}

then use a bang to tell the compiler your stores are present:

public render() {
const {a, b} = this.store!

}

This is truly inelegant. Below you will find a much better setup to have Mobx and React play nice with TypeScript.

TLDR;

import React from 'react';
import { inject, IWrappedComponent, observer } from 'mobx-react';
import { MyStoreClass } from './myStore';

type StoreProps = {
myStore: MyStoreClass;
};

interface Props extends StoreProps {
realProp: string;
}

@inject('myStore')
@observer
class App extends React.Component<Props> {
static defaultProps = {} as StoreProps;

render() {
const { myStore, realProp } = this.props;
[...]
}
}

export default App as typeof App & IWrappedComponent<Props>;

Working Example

Explanations and Steps to convert your code

Consider the following example, a simple counter app with a store injected through the context.


import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'mobx-react';
import counterStore from './counterStore';

import App from './App';

const rootElement = document.getElementById('root');
render(
<Provider counterStore={counterStore}>
<App title='normal prop title' />
</Provider>,
rootElement
);


import { observable } from 'mobx';

export class CounterStore {
@observable counter = 0;

increment() {
this.counter++;
}

decrement() {
this.counter--;
}
}

export default new CounterStore();


import React from 'react';
import { inject, observer } from 'mobx-react';

@inject('counterStore')
@observer
class App extends React.Component {
render() {
const { counterStore, title } = this.props;

return (
<div>
<div>{title}</div>
<button onClick={() => counterStore.increment()}>+1</button>
<span>{counterStore.counter}</span>
<button onClick={() => counterStore.decrement()}>-1</button>
</div>
);
}
}

export default App;

Converting to TypeScript

Important:

  • This will only work with Mobx 5 as v6 dropped decorators support. Discussion
  • You will also need to stay on mobx-react v6 to be compatible with mobx 5.
  • If you are starting from scratch and you can get away with only injecting your stores through hooks, you don’t need all this.

1. Filenames

Convert all the files to .ts and .tsx

2. Convert the store to TS

Pretty straightforward, add the type for counter:

[...]
@observable counter: number = 0;
[...]

3. Typed Props for the App Component

Let’s split the injected stores props and the actual props (you will see why later).

import { CounterStore } from './counterStore';

type StoreProps = {
counterStore: CounterStore;
};

interface Props extends StoreProps {
title: string;
}

@inject('counterStore')
@observer
class App extends React.Component<Props> {
[...]
};

export default App;

The warnings in our App component are now gone but the index.ts file is complaining about the missing counterStore.

4. Fixing the App Component exported type

First of all we need to specify defaultProps so that the compiler does not expect us to pass the store down

@inject('counterStore')
@observer
class App extends React.Component<Props> {
static defaultProps = {} as StoreProps;

render() {
[...]

then we need to adjust the type of the default export (our Component). This will help TypeScript recognize the fact that App has a static wrappedComponent property.

This is especially useful in unit tests as you will probably want to test App.wrappedComponent instead of App.

Here is how to export the correct type:

export default App as (typeof App & IWrappedComponent<Props>);

Your app is now ready to go !

5. Full code


import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'mobx-react';
import store from './counterStore';

import App from './App';

const rootElement = document.getElementById('root');
render(
<Provider counterStore={store}>
<App title='normal prop title' />
</Provider>,
rootElement
);


import { observable } from 'mobx';

export class CounterStore {
@observable counter: number = 0;

increment() {
this.counter++;
}

decrement() {
this.counter--;
}
}

export default new CounterStore();


import React from 'react';
import { inject, IWrappedComponent, observer } from 'mobx-react';
import { CounterStore } from './counterStore';

type StoreProps = {
counterStore: CounterStore;
};

interface Props extends StoreProps {
title: string;
}

@inject('counterStore')
@observer
class App extends React.Component<Props> {
static defaultProps = {} as StoreProps;

render() {
const { counterStore, title } = this.props;

return (
<div>
<div>{title}</div>
<button onClick={() => counterStore.increment()}>+1</button>
<span>{counterStore.counter}</span>
<button onClick={() => counterStore.decrement()}>-1</button>
</div>
);
}
}

export default App as typeof App & IWrappedComponent<Props>;