reactjs - 我正在尝试使用 rowEvents 触发 react-bootstrap-table-2 中的操作,但 onClick 中的 'this' 未定义。有任何想法吗?

标签 reactjs onclick this react-bootstrap-table

这是代码块:

const expandRow = {
    renderer: row => (
        <div>
            <BootstrapTable
                keyField='id'
                data={ row.data }
                columns={ columns1 }
            />
        </div>
    )
};

export default class MyPage extends Component {
    constructor(props, context) {
        super(props, context);
    }

    componentDidMount() {
        this.props.actions.fetchData().  // fetches in this.props.data
    }

    rowEvents = {
        onClick: (e, row, rowIndex) => {
            this.props.actions.fetchHistory(row.Id).   /* this is undefined */
        }
    }

    render() {

        return (
            <BootstrapTable
                keyField='Id'
                data={ this.props.data }
                columns={ columns }
                striped
                rowEvents={ this.rowEvents }
                expandRow={ expandRow }
            />
        )
    }
}
我想要做的是对于点击的每一行,我想通过触发一个 Action 来获取数据。我在这里理解“这个”的上下文可能是错误的。有任何想法吗?

最佳答案

rowEvents 不是函数而是常量,尝试用 const 定义

const rowEvents = {
        onClick: (e, row, rowIndex) => {
            this.props.actions.fetchHistory(row.Id).   /* this is undefined */
        }
    }

关于reactjs - 我正在尝试使用 rowEvents 触发 react-bootstrap-table-2 中的操作,但 onClick 中的 'this' 未定义。有任何想法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63080891/

相关文章:

ajax - 我如何在 jQuery 上引用该对象?

c++ - 在链表中实现高级方法,无需 const 强制转换

reactjs - 在 Heroku 上部署时找不到模块错误

javascript - 当子节点可以是 React 节点或函数时如何使用 React.FC<props> 类型

jquery - 在 jQuery 中,为什么以编程方式触发复选框上的 'click()' 不会立即检查它?

Javascript setTimeout 不起作用,我做错了什么?

javascript - react 事件冒泡 : Finding the target Component

javascript - 比较2个对象数组中的值并创建新数组,js

javascript - 西格玛 JS : how to keep edge selected onclick

javascript - 在 for 循环 JavaScript 中单击时更改 div 的 CSS