为什么当我使用history.push()时我的状态没有更新?

问题描述 投票:1回答:1

我这样使用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被更改了。但是当我删除它时,我的状态更新了请帮助我

reactjs browser-history
1个回答
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) }) }


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) })

© www.soinside.com 2019 - 2024. All rights reserved.