-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {makeAutoObservable} from 'mobx'; | ||
import {globalStore} from '../global'; | ||
|
||
class About { | ||
constructor() { | ||
makeAutoObservable(this); | ||
} | ||
count = 0; | ||
aboutAddCount = () => { | ||
const {count: globalCount} = globalStore; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
this.count += globalCount; | ||
}; | ||
} | ||
|
||
const aboutStore = new About(); | ||
export {aboutStore}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {makeAutoObservable} from 'mobx'; | ||
|
||
class Global { | ||
constructor() { | ||
makeAutoObservable(this); | ||
This comment has been minimized.
Sorry, something went wrong.
Oreo-Undefined
|
||
} | ||
count = 0; | ||
name = 'react'; | ||
addCount = () => { | ||
this.count++; | ||
This comment has been minimized.
Sorry, something went wrong.
Oreo-Undefined
|
||
}; | ||
setName = (data: string) => { | ||
this.name = data; | ||
}; | ||
} | ||
|
||
const globalStore = new Global(); | ||
export {globalStore}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as React from 'react'; | ||
import {configure} from 'mobx'; | ||
import {globalStore} from './global'; | ||
import {aboutStore} from './about'; | ||
|
||
configure({enforceActions: 'always'}); // 任何状态都能只能通过actions来修改,在实际开发中也包括新建状态。 | ||
|
||
This comment has been minimized.
Sorry, something went wrong. |
||
export const stores = {globalStore, aboutStore}; | ||
|
||
export const storesContext = React.createContext(stores); | ||
This comment has been minimized.
Sorry, something went wrong.
Oreo-Undefined
|
||
|
||
export const useStores = () => React.useContext(storesContext); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
import React from 'react'; | ||
import {observer} from 'mobx-react-lite'; | ||
import {useStores} from '@/store'; | ||
import './index.less'; | ||
|
||
const About = () => { | ||
const {globalStore} = useStores(); | ||
const {count, name} = globalStore; | ||
This comment has been minimized.
Sorry, something went wrong.
Oreo-Undefined
|
||
return ( | ||
<div className='about-root'> | ||
<p>Hello About</p> | ||
<p>{name}</p> | ||
<p>{count}</p> | ||
</div> | ||
); | ||
}; | ||
|
||
export default About; | ||
export default observer(About); | ||
This comment has been minimized.
Sorry, something went wrong. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,21 @@ | ||
import React from 'react'; | ||
import {observer} from 'mobx-react-lite'; | ||
import {useStores} from '@/store'; | ||
import './index.less'; | ||
|
||
const HomeOne = () => { | ||
return <div className='home-one-root'>HomeOne</div>; | ||
const {globalStore, aboutStore} = useStores(); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
const {count, name} = globalStore; | ||
const {count: aboutCount} = aboutStore; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
return ( | ||
<div className='home-one-root'> | ||
HomeOne | ||
<p>{name}</p> | ||
<p> globalStore: {count}</p> | ||
<p> aboutStore: {aboutCount}</p> | ||
</div> | ||
); | ||
}; | ||
|
||
export default HomeOne; | ||
export default observer(HomeOne); |
接某处解构的评论:这里的解构其实就没问题,因为没有 track 的场景