Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: Can't call setState (or forceUpdate) on an unmounted component

I am getting this warning every time I sign in,

Warning: Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.

Here is my code:

authpage.js

 handleLoginSubmit = (e) => {
    e.preventDefault()
    let { email,password } = this.state
    const data = {
      email : email,
      password : password
    }
    fetch('http://localhost:3001/auth/login',{
      method : 'post',
      body : JSON.stringify(data),
      headers : {
        "Content-Type":"application/json"
      }
    }).then(res => res.json())
    .then(data => {
      if(data.success){
        sessionStorage.setItem('userid',data.user.id)
        sessionStorage.setItem('email',data.user.email)
      }
      this.setState({loginData : data,
                     userData : data,
                     email:"",
                     password:""})
      if(data.token) {
        Auth.authenticateUser(data.token)
        this.props.history.push('/dashboard')
      }
      this.handleLoginMessage()
      this.isUserAuthenticated()
    })
  }

export default withRouter(AuthPage)

use withRouter so I can access props which I use to navigate this.props.history.push('/dashboard') if I didn't use withRouter I cannot access this.props

index.js

const PrivateRoute = ({ component : Component, ...rest }) => {
  return (
    <Route {...rest} render = { props => (
    Auth.isUserAuthenticated() ? (
      <Component {...props} {...rest} />
    ) : (
      <Redirect to = {{
        pathname: '/',
        state: { from: props.location }
      }}/>
    )
  )}/>
  )
}

const PropsRoute = ({ component : Component, ...rest }) => (
  <Route {...rest} render = { props => (
    <Component {...props} {...rest} />
  )}/>
)

const Root = () => (
  <BrowserRouter>
    <Switch>
      <PropsRoute exact path = "/" component = { AuthPage } />
      <PrivateRoute path = "/dashboard" component = { DashboardPage } />
      <Route path = "/logout" component = { LogoutFunction } />
      <Route component = { () => <h1> Page Not Found </h1> } />
    </Switch>
  </BrowserRouter>
)

I think the problem is with my withRouter, how can we access this.props without using withRouter ?

like image 215
tony chen Avatar asked May 24 '18 08:05

tony chen


1 Answers

It's async so

this.setState({
   loginData : data,
   userData : data,
   email:"",
   password:""
})

make error You can use this._mount to check component is mounted or unmount

componentDidMount () {
   this._mounted = true
}
componentWillUnmount () {
   this._mounted = false
}
...
if(this._mounted) {
    this.setState({
        loginData : data,
        userData : data,
        email:"",
        password:""
})
...
like image 71
nguyen nani Avatar answered Nov 15 '22 14:11

nguyen nani