我这样使用history.push:
state = {
resumeList : [],
}
Searchinp = (event) => {
event.preventDefault()
const props = this.props
console.log(props)
const searchValue = event.target.value
this.setState({
Search:searchValue
})
console.log(searchValue)
props.history.push({search: `phrase=${searchValue}` });
reqhandler({
url: exporturl.getportofoliorequest(),
method : "get",
headers : {
"Authorization": `Bearer ${gettoken().acctoken}`
},
params : {
lang : getLang(),
phrase : event.target.search.value
}
}).then(res => {
console.log(res.data)
this.setState({
...this.state,
resumeList : res.data.results,
})
console.log(this.state.resumeList)
}).catch(err => {
console.log("Error :" , err)
})
}
当我使用history.poush()时,我的状态没有更新,但是url被更改了。 但是当我删除它时,我的状态更新了 请帮助我
答案 0 :(得分:1)
history.push将您发送到另一个页面(重定向),因此它将停止 执行功能
始终在功能末尾(或要完成功能的地方)使用history.push
state = {
resumeList : [],
}
Searchinp = (event) => {
event.preventDefault()
const props = this.props
console.log(props)
const searchValue = event.target.value
this.setState({
Search:searchValue
})
console.log(searchValue)
reqhandler({
url: exporturl.getportofoliorequest(),
method : "get",
headers : {
"Authorization": `Bearer ${gettoken().acctoken}`
},
params : {
lang : getLang(),
phrase : event.target.search.value
}
}).then(res => {
console.log(res.data)
this.setState({
...this.state,
resumeList : res.data.results,
})
props.history.push({search: `phrase=${searchValue}` });
console.log(this.state.resumeList)
}).catch(err => {
console.log("Error :" , err)
})
}
答案 1 :(得分:0)
在更新状态后导航到其他组件作为回调。
reqhandler({
url: exporturl.getportofoliorequest(),
method : "get",
headers : {
"Authorization": `Bearer ${gettoken().acctoken}`
},
params : {
lang : getLang(),
phrase : event.target.search.value
}
}).then(res => {
console.log(res.data)
this.setState({
...this.state,
resumeList : res.data.results,
},{
props.history.push({search: `phrase=${searchValue}` });
})
}).catch(err => {
console.log("Error :" , err)
})