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

Pra*_*JaL 6 onclick this reactjs 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 }
            />
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是对于点击的每一行,我想通过触发一个动作来获取数据。我在这里理解“这个”的上下文可能是错误的。有任何想法吗?

Pra*_*JaL 2

我找到了解决“this”上下文问题的方法。rowEvents以稍微不同的方式实现,如下所示。我仍然不太清楚为什么我遇到了这样的上下文问题。

let rowEvents = () => {
    return{
    onClick: rowOnClick
    }
}
async function rowOnClick(e, row, rowIndex) {
    await this.props.actions.fetchLogHistory(row.orchestratorId)
}

and bind rowOnClick to this in the constructor:
this.rowOnClick = this.rowOnClick.bind(this)
Run Code Online (Sandbox Code Playgroud)

然而,虽然我使用 async/await 来触发操作,但我遇到了另一个问题,即在操作减少之前加载了扩展行(内部)引导表,并且 props 中没有数据。我现在尝试的是使用Component扩展行中渲染的另一个,在其中,我将拥有内部引导表,它将componenDidMount获取数据。