Skip to content

Commit

Permalink
test(mobx): 添加mobx示例子组件
Browse files Browse the repository at this point in the history
  • Loading branch information
LzhengH committed Feb 12, 2025
1 parent e92d830 commit 353798a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/button/_example/mobx.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'tdesign-web-components/button';
import 'tdesign-web-components/space';
import 'tdesign-web-components/input';
import './mobxChild';

import { autorun } from 'mobx';
import { Component } from 'omi';
Expand All @@ -22,7 +23,6 @@ export default class TestMobx extends Component {
});
};

// 使用autorun来响应状态变化
installed() {
console.log('installed');
autorun(() => {
Expand All @@ -39,6 +39,7 @@ export default class TestMobx extends Component {
</t-button>
<t-input value={myStore.message} onChange={this.inputChange} />
{myStore.message}
<t-mobx-child />
</t-space>
);
}
Expand Down
23 changes: 23 additions & 0 deletions src/button/_example/mobxChild.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'tdesign-web-components/button';
import 'tdesign-web-components/space';

import { autorun } from 'mobx';
import { Component, tag } from 'omi';

import { myStore } from '../store';

@tag('t-mobx-child')
export default class TestMobxChild extends Component {
// 子组件的autorun可以注掉
installed() {
// 如果父子组件都调了autorun,会多次触发,看是否优化
autorun(() => {
this.update();
console.log('子组件触发autorun');
});
}

render() {
return <t-space>mobx子组件:{myStore.message}</t-space>;
}
}

0 comments on commit 353798a

Please sign in to comment.