react的状态点击事件的问题

发布于 2022-09-11 17:01:36 字数 1172 浏览 15 评论 0

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

}
这么写就失效了

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请帮我爱他 2022-09-18 17:01:36

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

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

然后调用

this.props.store.changeBarTitle()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文