Skip to content

Commit ff650fc

Browse files
ericmasielloEric Masiello
and
Eric Masiello
authored
Adds MobX / MobX React Lite (#75)
* feat: add mobx * docs: fix typo * docs: update README with mobx results * refactor: rename mobx to mobx-react-lite for consistency with react binding names --------- Co-authored-by: Eric Masiello <[email protected]>
1 parent 620e09d commit ff650fc

File tree

7 files changed

+266
-183
lines changed

7 files changed

+266
-183
lines changed

README.md

Lines changed: 212 additions & 181 deletions
Large diffs are not rendered by default.

__tests__/all_spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const names = [
3333
'react-rxjs',
3434
'simplux',
3535
'react-query',
36+
'mobx-react-lite',
3637
];
3738

3839
names.forEach((name) => {

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"build:effector": "cross-env NAME=effector webpack",
3636
"build:react-rxjs": "cross-env NAME=react-rxjs webpack",
3737
"build:valtio": "cross-env NAME=valtio webpack",
38+
"build:mobx-react-lite": "cross-env NAME=mobx-react-lite webpack",
3839
"build-all": "run-s build:*"
3940
},
4041
"keywords": [
@@ -53,6 +54,8 @@
5354
"effector-react": "^22.4.0",
5455
"graphql": "^16.6.0",
5556
"jotai": "^2.0.0",
57+
"mobx": "^6.8.0",
58+
"mobx-react-lite": "^3.4.0",
5659
"react": "^18.2.0",
5760
"react-dom": "^18.2.0",
5861
"react-hooks-global-state": "^2.1.0",

src/common.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export const createApp = (
6767
useDouble,
6868
Root = React.Fragment,
6969
componentWrapper = React.memo,
70+
mainWrapper = (fn) => fn,
7071
) => {
7172
const Counter = componentWrapper(() => {
7273
const count = useCount();
@@ -80,7 +81,7 @@ export const createApp = (
8081
return <div className="count">{count}</div>;
8182
});
8283

83-
const Main = () => {
84+
const Main = mainWrapper(() => {
8485
const [isPending, startTransition] = useTransition();
8586
const [mode, setMode] = useState(null);
8687
const transitionHide = () => {
@@ -142,7 +143,7 @@ export const createApp = (
142143
<div id="mainCount" className="count">{mode === 'deferred' ? deferredCount : count}</div>
143144
</div>
144145
);
145-
};
146+
});
146147

147148
const App = () => (
148149
<Root>

src/mobx-react-lite/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React, { useCallback } from 'react';
2+
import { observable, runInAction } from 'mobx';
3+
import { observer } from 'mobx-react-lite';
4+
5+
import {
6+
reducer,
7+
initialState,
8+
selectCount,
9+
incrementAction,
10+
doubleAction,
11+
createApp,
12+
} from '../common';
13+
14+
const state = observable(initialState);
15+
16+
const useCount = () => selectCount(state);
17+
18+
const useIncrement = () => useCallback(() => {
19+
const newState = reducer(state, incrementAction);
20+
runInAction(() => {
21+
Object.keys(newState).forEach((key) => {
22+
state[key] = newState[key];
23+
});
24+
});
25+
}, []);
26+
27+
const useDouble = () => useCallback(() => {
28+
const newState = reducer(state, doubleAction);
29+
runInAction(() => {
30+
Object.keys(newState).forEach((key) => {
31+
state[key] = newState[key];
32+
});
33+
});
34+
}, []);
35+
36+
export default createApp(useCount, useIncrement, useDouble, React.Fragment, observer, observer);

update_readme.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const libraries = {
1919
'react-rxjs': '<a href="https://react-rxjs.org">react-rxjs</a>',
2020
simplux: '<a href="https://github.com/MrWolfZ/simplux">simplux</a>',
2121
'react-query': '<a href="https://react-query.tanstack.com/">react-query</a>',
22+
'mobx-react-lite': '<a href="https://github.com/mobxjs/mobx-react-lite">mobx-react-lite</a>',
2223
};
2324

2425
const numTests = 10;

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5204,6 +5204,16 @@ mkdirp@^0.5.6:
52045204
dependencies:
52055205
minimist "^1.2.6"
52065206

5207+
mobx-react-lite@^3.4.0:
5208+
version "3.4.0"
5209+
resolved "https://registry.yarnpkg.com/mobx-react-lite/-/mobx-react-lite-3.4.0.tgz#d59156a96889cdadad751e5e4dab95f28926dfff"
5210+
integrity sha512-bRuZp3C0itgLKHu/VNxi66DN/XVkQG7xtoBVWxpvC5FhAqbOCP21+nPhULjnzEqd7xBMybp6KwytdUpZKEgpIQ==
5211+
5212+
mobx@^6.8.0:
5213+
version "6.8.0"
5214+
resolved "https://registry.yarnpkg.com/mobx/-/mobx-6.8.0.tgz#59051755fdb5c8a9f3f2e0a9b6abaf86bab7f843"
5215+
integrity sha512-+o/DrHa4zykFMSKfS8Z+CPSEg5LW9tSNGTuN8o6MF1GKxlfkSHSeJn5UtgxvPkGgaouplnrLXCF+duAsmm6FHQ==
5216+
52075217
52085218
version "2.0.0"
52095219
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"

0 commit comments

Comments
 (0)