react的状态点击事件的问题

新手上路,请多包涵

最近在使用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

}
这么写就失效了

阅读 1.7k
1 个回答

action是要写在 store 中的,然后在需要用到 store 的组件中,先引入

@inject(stores => ({ ...stores }))
@observer

然后调用

this.props.store.changeBarTitle()
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题