Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

reactjs - Re-initializing class on redirect

I am currently at http://example.com/parentdir/module/2/

This URL actually loads the Module.js class as indicated in the following route:

<Route exact path={"/parentdir/module/:id"} component={Module} />

This is contained within <BrowserRouter> and <Switch> elements as defined in my index.js file.

The Module class has a constructor and componentWillMount methods. These set up the initial details and load information for record #2. Everything works fine so far.

Now, my problem is that in a grandchild component of Module.js I redirect to another page such as page 3 with the following Redirect:

return (
        <Redirect to="/parentdir/module/3/" />
)

The constructor or componentWillMount do not run so record #3 does not properly load. Is this because this component is already loaded in memory from when record #2 was loaded? What is the best way to reload Module.js so it properly loads record #3? Do I somehow have to reinitialize the class? Or do I somehow have to pass the data from the grandchild component back up to the module class? The latter approach would seem to be tedious.

Just to clarify, if I go directly to http://example.com/parentdir/module/3 in the address bar everything works properly, but if I redirect to this same address within the ReactJS environment it does not work properly.

Update

Two answers to my question have been given so far. One solution suggests using componentWillReceiveProps. The other solution suggests passing a method as a prop down to the child and then to the grandchild. Are there pros and cons to using each of these approaches or is it just a matter of preference?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The id is passed as a prop in the form of this.props.params.id so you should use the componentWillReceiveProps lifecycle method which runs everytime the props change which is what is happening in your case.

The componentWillMount method will not run when you navigate from /parentdir/module/2 to /parentdir/module/3 as the component is already mounted. It will only run when you navigate from some other component (when you go directly for example).

In your Module component add this lifecycle method

componentWillReceiveProps(nextProps){
      if(nextProps.params.id != this.props.params.id){
          //load information etc (whatever you are doing in componentWillMount)
       }
}

What is happening is that when it receives the newer set of props it's comparing if the param id has changed (all route params live in the params object in props) and when it sees there is a change it carries out the logic. The comparison is not really necessary and you can add the logic as is in the componentWillReceiveProps method but that would be inefficient as it is called everytime the props object changes (might not be the id param at all).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...