【问题标题】:why my state didn't update when i use history.push()?为什么当我使用 history.push() 时我的状态没有更新?
【发布时间】:2019-12-23 13:02:01
【问题描述】:

我像这样使用 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】:

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

    【讨论】:

    • 状态已更新,我的问题是:映射到表格中的状态没有显示任何内容,您能指导我吗?
    【解决方案2】:

    更新状态后作为回调导航到其他组件。

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

    【讨论】:

    • 状态已更新,我的问题是:映射到表格中的状态没有显示任何内容,您能指导我吗?
    猜你喜欢
    • 1970-01-01
    • 2017-01-25
    • 2023-03-31
    • 2011-12-14
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    相关资源
    最近更新 更多