最近在使用react解决点击事件的时候发现使用state用setstate效果是好的,换成mobx后失效
下面上代码:
import React, {Component} from 'react'
import {Card, Button} from 'antd'
import {BarList} from "./inventoryStore";
import * as styles from './style.pcss';
import {InventoryTable} from "./inventoryTable";
class BarExtra extends Component<any, any> {
constructor(props) {
super(props)
this.state = {
barTitle: '列表数据'
}
}
onClick = item => e => {
// inventoryStore.changeBarTitle(item)
this.setState({
barTitle: item
})
}
render() {
const _style = {
color: '#1890ff'
};
return (
<>
{
BarList.map((item, index) => {
return<a key={index}
className={styles.bar}
onClick={this.onClick(item)}
style={item === this.state.barTitle? _style : {} }
>
<span>{item}</span>
</a>
})
}
</>
)
}
}
如图这样写是好的但是换成mobx中
@action changeBarTitle(item) {
this.barTitle = item
}
这么写就失效了
action是要写在 store 中的,然后在需要用到 store 的组件中,先引入
然后调用