react-keeper icon indicating copy to clipboard operation
react-keeper copied to clipboard

constructor and componentDidMount is not called multiple times

Open reneeichhorn opened this issue 8 years ago • 9 comments

Issue

When I change the example for the Detail page to:

import React , { Component } from 'react'

class Detail extends Component {
  constructor(props) {
    super(props)
    console.error('detail: constructor')
  }

  componentDidMount() {
    console.error('detail: component did mount')
  }

  componentWillUnmount() {
    console.error('detail: component will unmount')
  }

  render() {
    console.error('detail: render')

    const props = this.props

    return (
      <div>
        <hr/>
        Detail<br/>
        id: {props.params ? props.params.id : null}<br/>
        time: {props.time}
      </div>
    )
  }
}
export default Detail

The first time I go to a route with the detail component it will show

detail: constructor
detail: component did mount
detail: render

the next route will only print:

detail: render

Expected behavior:

The next route should cause a new component to be initialized (it shouldn't reuse the original one) and then execute componentDidMount.

PAGE INIT:
detail: constructor
detail: component did mount
detail: render
NEXT PAGE:
detail: constructor
detail: component did mount
detail: render
GO BACK:
- nothing if cached -

Why?

Even in the official react documentation it is stated that componentDidMount is a good place to do api requests to load content. This doesn't work right now.

reneeichhorn avatar Apr 12 '17 13:04 reneeichhorn

Found out that this can be solved by adding the key prop to the Route component. props.key = this.state.selfPathname before React.createElement(...) There is no way to pass a unique key depending on the route currently (from the outside)

reneeichhorn avatar Apr 12 '17 14:04 reneeichhorn

@reneeichhorn If there is a 'cache' property on your Route component? If so, the Detail page will be cached instead of unmounted when url is not matching.

lanistor avatar Apr 12 '17 16:04 lanistor

It has one, I'm just using the example given in the repository @vifird

reneeichhorn avatar Apr 12 '17 16:04 reneeichhorn

The main issue is that without the key prop react won't understand what is a new component and what not. Therefore it will just think every route is just the same component. Even when going into a new route.

reneeichhorn avatar Apr 12 '17 16:04 reneeichhorn

Yeah, key property is used as ID of a component. @reneeichhorn

lanistor avatar Apr 12 '17 16:04 lanistor

I will give you an example: Imagine a /product/:id route that uses the Product component. The product component uses componentDidMount to recieve data from the server. (as suggested by react best practices)

Now I navigate to /product/123. Product component is mounted, data for product with id 123 is fetched. Product component get rendered again after retrieving data. Next I navigate to /product/456. Here is where the issue starts (the following problem appear WITH cache and also WITHOUT cache). The Product component is just rerendered with different props. componentDidMount is not executed again and therefore no new data is retrieved. Product will still display data for the 123 product because new data was not fetched.

With or without caching this should not happen. Without a proper key prop on the Route for React it will look like the same component just with different props, therefore only mounted once. This completely breaks React. And forces us to write workarounds.

reneeichhorn avatar Apr 12 '17 17:04 reneeichhorn

@reneeichhorn Yeah, thanks for your issue. We are repairing this problem.

lanistor avatar Apr 14 '17 02:04 lanistor

Good one: next version 3

lanistor avatar Mar 04 '18 08:03 lanistor

也遇到了同样的问题,什么时候发布新版本啊?????

koupeng avatar Mar 29 '18 02:03 koupeng