我正在尝试使用 rowEvents 触发 react-bootstrap-table-2 中的操作,但 onClick 中的 'this' 未定义。有任何想法吗?
I am trying to use rowEvents to trigger an action in react-bootstrap-table-2 but 'this' in the onClick is undefined. Any ideas?
这是代码块:
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 }
/>
)
}
}
我想做的是针对点击的每一行,我想通过触发一个动作来获取数据。我在这里理解 'this' 的上下文可能是错误的。有什么想法吗?
rowEvents不是函数而是常量,尝试用const定义
const rowEvents = {
onClick: (e, row, rowIndex) => {
this.props.actions.fetchHistory(row.Id). /* this is undefined */
}
}
我找到了解决 '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)
然而,虽然我使用 async/await 来触发动作,但我 运行 进入另一个问题,其中扩展行(内部) bootstrap table 在动作之前加载减少了,道具中没有数据。我现在尝试的是使用另一个呈现在扩展行中的 Component
,在其中,我将拥有内部 bootstrap table,其 componenDidMount
将获取数据.
这是代码块:
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 }
/>
)
}
}
我想做的是针对点击的每一行,我想通过触发一个动作来获取数据。我在这里理解 'this' 的上下文可能是错误的。有什么想法吗?
rowEvents不是函数而是常量,尝试用const定义
const rowEvents = {
onClick: (e, row, rowIndex) => {
this.props.actions.fetchHistory(row.Id). /* this is undefined */
}
}
我找到了解决 '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)
然而,虽然我使用 async/await 来触发动作,但我 运行 进入另一个问题,其中扩展行(内部) bootstrap table 在动作之前加载减少了,道具中没有数据。我现在尝试的是使用另一个呈现在扩展行中的 Component
,在其中,我将拥有内部 bootstrap table,其 componenDidMount
将获取数据.