代码之家  ›  专栏  ›  技术社区  ›  Sooraj

React router`this.props.history.push`-未定义历史记录

  •  0
  • Sooraj  · 技术社区  · 6 年前

    我有这个注销功能:

    logOut = () => {
        axios
          .delete(`${apiUrl}/logout`)
          .then(response => {
            if (response.status === 204) {
              localStorage.removeItem('key', response.headers.authorization)
              localStorage.removeItem('id', response.data.id)
              // return <Redirect to="/login" />
              this.props.history.push(`/login`)
            }
          })
          .catch(error => {
            console.log(error)
          })
      }
    

    this.props.history.push('/something') 在其他场合都有用。但这里写的是不识字 push 未定义的。我试过了 return <Redirect to="/login" /> 这也不管用。

    我做错什么了?

    1 回复  |  直到 6 年前
        1
  •  14
  •   Pouya Jabbarisani    6 年前

    可能是因为以下原因之一:

        import { withRouter } from 'react-router-dom
    
       // ... codes of component in here ...
    
        export default withRouter(componentName) // at the end of component
    

    var that = this;
    
     // then use that in axios