React project, when a user enters an interface, he / she needs to judge whether the user's information exists. If not, he / she will request.

problem description

1. No matter what interface users enter, they all need to obtain user information and the function shared by Wechat
2. If users have already obtained certain information, they will no longer be transferred to the request interface for this information

.

the environmental background of the problems and what methods you have tried

react project, Wechat official account

related codes

index.js entry file

ReactDOM.render(
    <Provider store={store}>
        <Route />
    </Provider>
    ,
    document.getElementById("root"));
registerServiceWorker();

router/index.js

export const routes = [{
    path: "/x",
    component: () => import("../xx")
}, {
    path: "/xx",
    component: () => import("../xx")
}, 
...
]
const router = (props) => (
    <BrowserRouter>
        <Switch>
            <Route exact path="/" render={() => <Redirect to="/invite" />} />
            {routes.map(({ path, title, component }, index) => (
                <Route
                    key={index}
                    exact
                    path={path}
                    onEnter={() => { document.title = title }}
                    component={AsyncComponent(component)}
                />
            ))}
        </Switch>
    </BrowserRouter>
)

Home.js Home

componentDidMount() {
    const { dispatch } = this.props
    get(url).then(data => {
      if (!data) {
        window.location.href = "xxx"
      } else {
        dispatch({
          type: xxx,
          openid: xxx
          avatar: xxx,
          name: xxx
        })

        post(url, params).then(data => {
          if(!data){
            this.props.history.push("/");
            return false;
          }
          
          window.wx.config({
            debug: true, 
            appId: data.appId, 
            timestamp: data.timestamp, 
            nonceStr: data.nonceStr,
            signature: data.signature,
            jsApiList: data.jsApiList
          });

          window.wx.ready(function () {
            // 
            window.wx.onMenuShareTimeline(shareData);

          });
          ...
        })
      }
    })
  }

what result do you expect? What is the error message actually seen?

at present, I write down the configuration of obtaining user information and sharing by Wechat on the home page, but find that when users enter other addresses from the address bar (or refresh them in other routing interfaces), For example, when the route http://xx.com/an enters (refresh), you cannot get user information and Wechat"s configuration information
I think no matter which interface the user enters or refreshes, Both the user"s information and Wechat"s configuration information can be obtained

[question]:
1. Where can I write the js of componentDidMount in home.js to avoid this problem? Is there any other solution?
2. At present, I have written the js of componentDidMount in home.js in router/index.js. Is this method common in the company"s official projects?

not long after I first came into contact with React, I asked the great gods to give me some ideas. Thank you

.

write an authenticated component separately, and any address will be routed to this component. Authentication is performed in componentDidMount of this component, but the component does not render any views

render () {
       return null
   }

this is how routes should be written

<BrowserRouter>
        <AuthRoute/>
        <Switch>
            <Route exact path="/" render={() => <Redirect to="/invite" />} />
            {routes.map(({ path, title, component }, index) => (
                <Route
                    key={index}
                    exact
                    path={path}
                    onEnter={() => { document.title = title }}
                    component={AsyncComponent(component)}
                />
            ))}
        </Switch>
    </BrowserRouter>

< AuthRoute/ > is a component of authentication


can you consider cookie? The specific operation of
is as follows. Add a user information authentication before the routing component in the component that is higher in the react hierarchy.
put the user information into the cookie. If the cookie exists, if the cookie does not exist, return to the login interface. After logging in, the user information will continue to be placed in the cookie. If the user refreshes after a long time, it is reasonable to exit to the login interface for security reasons.

Menu