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
 //Write your own logic below.

When calling, just like this:

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

Or higher

 export default class Component extends React.Component {