P
P
Pogran2016-09-01 09:08:38
React
Pogran, 2016-09-01 09:08:38

Why doesn't componentWillMount work?

I have such routing.

<Route path="entity" component={EntityForm} />
  <Route path="entity/edit/:entity_id" component={EntityForm} />

The point is that in the first case, we just load an empty form to create an element, and in the second case, an element is loaded for editing.
In EntityForm I have this code
componentWillMount() {
    if(!isEmpty(this.props.params.entity_id)) {
      this.props.loadEntityRequest(this.props.params.entity_id).then(() => {
        this.setState({name: this.props.entity.name});
      });
    } else {
      this.props.initEntity();
    }
  }

And when I am on the page being edited, i.e. on the page of this route entity/edit/:entity_id and go to the entity page (i.e. I try to create a new route), componentWillMount does not work for me. And componentWillMount either resets the state, or if the page is editing, loading an existing element. What could be my mistake in logic?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2016-09-01
@Pogran

The thing is, React is "smart". It understands that the same component needs to be rendered and does not replace it.
In this case, you can get the new props in the componentWillReceiveProps method and do a setState() there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question