0

I have an issue with setState().

Here is my routes.tsx file

export const Routes = (props:any) => (
<Router {...props}>
<Route path="/" component={Miramir}>
  <Route path="/profile">
    <IndexRoute component={Profile} />
    <Route path="/profile/update" component={ProfileUpdate} />
  </Route>
</Route>

So, when i trying to use /profile/update route I have an warning and see that component which only for /profile route exists on /profile/update

This is an error

Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the CountDown component.

Component CountDown

componentDidMount = () => {
    let timeLeft = this.secondsToTime(this.state.date);
    this.setState({time: timeLeft});
  }

_countDown = () => {
    this.setState({
      time: this.secondsToTime(this.state.date)
    })
  }

Calling _countDown every sec

Hope your help!

Thanks

4
  • 1
    I don'*t know if this solves your problem but nromally, paths are relative to the parent Route. so the route with path /profile/update is actually available at /profile/profile/update Commented Jan 20, 2017 at 12:10
  • Actually routes starting with / should be relative to the root, so my initial comment was wrong. But i always define my paths without starting / so they are relative and still work after changing a parent's path Commented Jan 20, 2017 at 12:18
  • Can you update the question to give more informations about your CountDown component? Where do you call setState()? Commented Jan 20, 2017 at 12:24
  • @MichaelRitter, updated Commented Jan 20, 2017 at 12:49

2 Answers 2

2

You probably call _countDown() via setInterval() I'd imagine. Did you clear that Interval in componentWillUnmount()?

Sign up to request clarification or add additional context in comments.

Comments

0

you can not call the _countDown before the component mount (it means the component render method is already called and then you calling _countDown function).

calling _countDown inside setInterval may solve, but may fail again if you render method take more time than time provided in setInterval.

_countDown = () => {
    this.setState({
      time: this.secondsToTime(this.state.date)
    })
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.