【发布时间】:2018-10-16 19:12:02
【问题描述】:
我在一个页面上有两个反应表。第一个显示已保存查询的列表。用户可以单击任何已保存的查询来运行查询结果,这些结果将呈现在第二个表中。我正在尝试根据“showQueryResults”的布尔状态显示/隐藏查询结果表。
当我在 getTDProps onClick 事件处理程序中将 showQueryResults 的状态设置为 false 时,没有任何反应。当我在页面的其他位置放置一个执行完全相同操作的测试按钮时,查询结果表被成功隐藏。
<ReactTable
data={this.props.data}
columns={columns}
getTdProps={(state, rowInfo, column) => {
return {
onClick: () => {
if (rowInfo){
const row = rowInfo.row
this.setState({selectedListId: row.id, showQueryResults: false},() => {
if(column.id !== 'delete'){
if (row.search_type === 'dynamic'){
this.props.fetchQueryResults(row._original.search_criteria)
.then(this.setState({showQueryResults: true}))
} else {
this.props.fetchStaticResults(row.id)
.then(this.setState({showQueryResults: true}))
}
}
});
}
},
}}}
/>
在我的主渲染函数中,我有条件地渲染查询结果如下:
{
this.state.showQueryResults
? <ListResults
queryResults = {this.props.queryResults}
onModalToggle = {this.handleModalToggle.bind(this)}
showSave = {false}
/>
: null
}
就像我说的,下面的测试按钮成功地隐藏了上面的元素:
<button onClick={() => this.setState({showQueryResults: !this.state.showQueryResults})}>Toggle</button>
有什么想法吗?
编辑:我还尝试了以下方法以确保 fetchQueryResults 在第二个 setState 之前完成(根据 cmets),但这样做完全相同:
<ReactTable
data={this.props.data}
columns={columns}
getTdProps={(state, rowInfo, column) => {
return {
onClick: () => {
if (rowInfo){
const row = rowInfo.row
const handleFetchQuery = (searchCriteria) => {
return new Promise((resolve)=>{
let response = this.props.fetchQueryResults(searchCriteria)
if (response){
resolve('Success')
}
})
}
this.setState({selectedListId: row.id, showQueryResults: false},() => {
if(column.id !== 'delete'){
if (row.search_type === 'dynamic'){
handleFetchQuery(row._original.search_criteria)
.then(() => {this.setState({showQueryResults: true})})
} else {
this.props.fetchStaticResults(row.id)
.then(this.setState({showQueryResults: true}))
}
}
});
}
},
}}}
【问题讨论】:
标签: javascript reactjs promise render react-table