-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,10 @@ usage: { title: '', description: '' } | |
spline: base | ||
--- | ||
|
||
### 测试mobx使用 | ||
|
||
{{ mobx }} | ||
|
||
### 基础使用 | ||
|
||
{{ base }} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import 'tdesign-web-components/button'; | ||
import 'tdesign-web-components/space'; | ||
import 'tdesign-web-components/input'; | ||
|
||
import { autorun } from 'mobx'; | ||
import { Component } from 'omi'; | ||
|
||
import { myStore } from '../store'; | ||
|
||
export default class TestMobx extends Component { | ||
btnClick = () => { | ||
console.log('btn click'); | ||
myStore.update({ | ||
message: '修改后', | ||
}); | ||
}; | ||
|
||
inputChange = (v: string) => { | ||
console.log(v); | ||
myStore.update({ | ||
message: v, | ||
}); | ||
}; | ||
|
||
// 使用autorun来响应状态变化 | ||
installed() { | ||
console.log('installed'); | ||
autorun(() => { | ||
this.update(); | ||
console.log('触发autorun'); | ||
}); | ||
} | ||
|
||
render() { | ||
return ( | ||
<t-space direction="vertical"> | ||
<t-button theme="default" variant="text" onClick={this.btnClick}> | ||
修改message | ||
</t-button> | ||
<t-input value={myStore.message} onChange={this.inputChange} /> | ||
{myStore.message} | ||
</t-space> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { makeAutoObservable } from 'mobx'; | ||
|
||
export class Store { | ||
message = 'Initial message'; | ||
|
||
constructor() { | ||
makeAutoObservable(this); | ||
} | ||
|
||
update = (props: Partial<Store>): void => { | ||
Object.assign(this, props); | ||
}; | ||
} | ||
|
||
export const myStore = new Store(); |