Taro 使用mobx
2020-03-31 本文已影响0人
blossom_6694
mobox 比 redux 更轻量,代码更清晰
例子:
import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'
import { observable } from 'mobx';
import { observer } from '@tarojs/mobx';
class TestStore {
@observable count: number = 0
}
@observer
export default class TestPage extends Component {
store = new TestStore()
handleClick() {
this.store.count ++
}
render() {
const { count } = this.store
return (<View style={{ display: 'flex', flexDirection: 'column', alignItems: 'center'}}>
<View>{count}</View>
<View onClick={this.handleClick.bind(this)}>add</View>
</View>);
}
}
点击add, count ➕1。
20200331173737.jpg