Gathering detailed insights and metrics for mobx-react-use-autorun
Gathering detailed insights and metrics for mobx-react-use-autorun
Gathering detailed insights and metrics for mobx-react-use-autorun
Gathering detailed insights and metrics for mobx-react-use-autorun
npm install mobx-react-use-autorun
Typescript
Module System
Node Version
NPM Version
TypeScript (77.93%)
JavaScript (19.92%)
Dockerfile (2.15%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
3 Stars
168 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Jul 14, 2025
Latest Version
4.0.60
Package Id
mobx-react-use-autorun@4.0.60
Unpacked Size
34.59 kB
Size
8.83 kB
File Count
38
NPM Version
10.9.2
Node Version
22.17.0
Published on
Jul 14, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
1
Provide concise usage for mobx in react
Installation
npm install mobx-react-use-autorun
Usage
import { observer, useMobxState } from 'mobx-react-use-autorun';
export default observer(() => {
const state = useMobxState({
age: 16
});
return <div
onClick={() => state.age++}
>
{`John's age is ${state.age}`}
</div>
})
More example - Form validation:
import { Button, TextField } from '@mui/material';
import { observer, useMobxState } from 'mobx-react-use-autorun';
export default observer(() => {
const state = useMobxState({
name: "",
submit: false,
errors: {
name() {
return state.submit &&
!state.name &&
"Please fill in the username";
},
hasError() {
return Object.keys(state.errors)
.filter(s => s !== "hasError")
.some(s => (state.errors as any)[s]());
}
}
});
async function ok(){
state.submit = true;
if (state.errors.hasError()) {
console.log("Submission Failed");
} else {
console.log("Submitted successfully");
}
}
return (<div className='flex flex-col' style={{ padding: "2em" }}>
<TextField
value={state.name}
label="Username"
onChange={(e) => state.name = e.target.value}
error={!!state.errors.name()}
helperText={state.errors.name()}
/>
<Button
variant="contained"
style={{ marginTop: "2em" }}
onClick={ok}
>Submit</Button>
</div>)
})
More example - Use props and other hooks:
import { observer, useMobxState } from 'mobx-react-use-autorun';
import { useRef } from 'react';
export default observer((props: { user: { name: string, age: number } }) => {
const state = useMobxState({
}, {
containerRef: useRef<HTMLDivElement>(null),
});
return <div
ref={state.containerRef}
onClick={() => {
props.user.age++;
}}
>
{`${props.user.name}'s age is ${props.user.age}.`}
</div>
})
useMount is a lifecycle hook that calls a function after the component is mounted.
It provides a subscription as parameter, which will be unsubscribed when the component will unmount.
It support Strict Mode.
Strict Mode: In the future, React will provide a feature that lets components preserve state between unmounts. To prepare for it, React 18 introduces a new development-only check to Strict Mode. React will automatically unmount and remount every component, whenever a component mounts for the first time, restoring the previous state on the second mount. If this breaks your app, consider removing Strict Mode until you can fix the components to be resilient to remounting with existing state.
import { Subscription } from 'rxjs'
import { observer, useMount } from 'mobx-react-use-autorun'
export default observer(() => {
useMount((subscription) => {
console.log('component is loaded')
subscription.add(new Subscription(() => {
console.log("component will unmount")
}))
})
return null;
})
import { useMobxState, observer } from 'mobx-react-use-autorun';
import { useMobxEffect, toJS } from 'mobx-react-use-autorun'
export default observer(() => {
const state = useMobxState({
randomNumber: 1
});
useMobxEffect(() => {
console.log(toJS(state))
}, [state.randomNumber])
return <div onClick={() => state.randomNumber = Math.random()}>
{state.randomNumber}
</div>
})
// File: GlobalState.tsx
import { observable } from 'mobx-react-use-autorun';
export const globalState = observable({
age: 15,
name: 'tom'
});
// File: UserComponent.tsx
import { observer } from "mobx-react-use-autorun";
import { globalState } from "./GlobalState";
export default observer(() => {
return <div
onClick={() => {
globalState.age++;
}}
>
{`${globalState.name}'s age is ${globalState.age}.`}
</div>;
})
"toJS" will cause the component to re-render when data changes.
Please do not execute "toJS(state)" in component rendering code, it may cause repeated rendering.
Wrong Example:
import { toJS, observer, useMobxState } from 'mobx-react-use-autorun'
import { v1 } from 'uuid'
export default observer(() => {
const state = useMobxState({}, {
id: v1()
})
toJS(state)
return null;
})
Other than that, all usages are correct.
Correct Example:
import { toJS, useMobxEffect } from 'mobx-react-use-autorun';
import { observer, useMobxState } from 'mobx-react-use-autorun';
import { v1 } from 'uuid'
export default observer(() => {
const state = useMobxState({
name: v1()
});
useMobxEffect(() => {
console.log(toJS(state));
console.log(toJS(state.name));
})
console.log(toJS(state.name));
return <button
onClick={() => {
console.log(toJS(state));
console.log(toJS(state.name));
}}
>
{'Click Me'}
</button>;
})
Non-observer components cannot trigger re-rendering when the following data changes, please use "toJS" to do it.
import { observer, toJS, useMobxState } from "mobx-react-use-autorun";
import { v1 } from "uuid";
import { Virtuoso } from 'react-virtuoso'
export default observer(() => {
const state = useMobxState({
userList: [{ id: 1, username: 'Tom' }]
})
toJS(state.userList)
return <Virtuoso
style={{ width: "500px", height: "200px" }}
data={state.userList}
itemContent={(index, item) =>
<div key={item.id} onClick={() => item.username = v1()}>
{item.username}
</div>
}
/>
})
Typedjson is a strongly typed reflection library.
import { makeAutoObservable, toJS } from "mobx-react-use-autorun";
import { TypedJSON, jsonMember, jsonObject } from "typedjson";
@jsonObject
export class UserModel {
@jsonMember(String)
username!: string;
@jsonMember(Date)
createDate!: Date;
constructor() {
makeAutoObservable(this);
}
}
const user = new TypedJSON(UserModel).parse(`{"username":"tom","createDate":"2023-04-13T04:21:59.262Z"}`);
console.log(toJS(user));
const anotherUser = new TypedJSON(UserModel).parse({
username: "tom",
createDate: "2023-04-13T04:21:59.262Z"
});
console.log(toJS(anotherUser));
This project was bootstrapped with Create React App. If you have any questions, please contact zdu.strong@gmail.com.
In the project directory, you can run:
npm test
Run all unit tests.
npm run build
Builds the files for production to the dist
folder.
npm run make
Publish to npm repository
Pre-step, please run
npm login --registry https://registry.npmjs.org
This project is licensed under the terms of the MIT license.
No vulnerabilities found.
No security vulnerabilities found.