在手动刷新页面之前,React Router路由组件将保持挂载状态

时间:2018-11-25 03:06:41

标签: javascript reactjs react-router

在下面的代码中,路由更改为“ /”(通过history.push("/"))后,Register组件仍停留在页面上。但是,如果我随后刷新页面,该页面将消失,并且一切看起来都应该如此。任何想法为什么会这样?

    <Router>
      <React.Fragment>
        <Route
          path="/register"
          exact
          render={() => <Register register={this.register} />}
        />
        <Route
          path="/"
          exact
          render={() => (
            <Main
              posts={posts}
              post={post}
              preview={preview}
            />
          )}
        />
      </React.Fragment>
    </Router>

这是负责推送到根路由的注册方法:

register = (e, credentials) => {
    e.preventDefault();
    if (authEmails.includes(credentials.email)) {
      rxcInit
        .auth()
        .createUserWithEmailAndPassword(credentials.email, credentials.password)
        .then(() => {
          history.push("/");
        })
        .catch(error => {
          var errorMessage = error.message;
          console.log(errorMessage);
          this.setState({ modalShowing: true, errorMsg: errorMessage });
        });
    } else {
      this.setState({
        modalShowing: true,
        errorMsg:
          "That email address isn't authorized."
      });
    }
  };

0 个答案:

没有答案