React plus react-router Method for Unified Treatment of All Components

  node.js, question

When I met a requirement, I had to deal with the same thing in every component. However, if dozens of components copy the same code into componentWillMount, the amount of duplication is too large at first, and if it needs to be modified, I will modify dozens of places. I tried the onEnter attribute of Route, but this attribute must be added to every Route. Is there any way for each great god to monitor route change events or bind a componentWillMount to all components at the same time? Please comment!

Write oneHOCBai, the following code insidesuperThe order of the call and its logic can be adjusted according to its own situation.

function HOC(wrapped) {
 return class Enhancer extends wrapped {
 componentWillMount() {
 //Call componentWillMount in wrapped
 super.componentWillMount();
 
 //Write your own logic below.
 bracket
 bracket
 bracket

When calling, just like this:

class Component extends React.Component {
 // bla...bla...bla  ...
 bracket
 
 export default HOC(Component);

Or higher

@HOC
 export default class Component extends React.Component {
 //...bla...bla...bla
 bracket