【问题标题】:Componets are overlapping - showing one over the other - react router v5组件重叠 - 显示一个在另一个上 - 反应路由器 v5
【发布时间】:2021-09-28 16:33:32
【问题描述】:

SignUp 组件和Home 组件重叠。
我有一条受保护的路线,用户将从SignUp 页面开始,当条件为真时(userSignedUp 正在注册时更新),它将转移到Home 组件,但SignUp 组件重叠并且一个活跃的人(输入和按钮)。

请检查- codesandbox - 项目。
(此处将history.push('/home') 添加到CreatePass 将在退出Home 后得到空白的sigup 组件)

任何人都知道如何解决这个问题,我的代码有什么问题?


App.js

 return (
    <div className='App-div'>
      <GuardedRoute path='/home' auth={userSignedUp}>
        <Home userIDNumber={userID} setIfSignUp={setIfSignUp} />
      </GuardedRoute>
      <Switch>
        <Route path='/signup'>
          <SignUp setUserNumber={setUserID} setIfSignUp={setIfSignUp} />
        </Route>
      </Switch>
    </div>
  );

GuardedRoute.js

function GuardedRoute({ children, auth }) {
  return (
    <Route
      render={() => {
        return auth ? children : <Redirect to='/signup' />;
      }}
    />
  );
}

在发布答案之前,请在我的代码框尝试您的任何解决方案

【问题讨论】:

  • 嘿!在浏览你的代码时,看起来你已经使用了没有路由器的 Route 和 GuardedRoute,用路由器包围代码(如果这是你第一次,我会推荐 HashRouter,因为其他路由器在刷新期间需要路由帮助
  • @AdityaRastogi 对不起,我不明白。你检查了我的 Index.js 和 SignUp.jsx 吗?它们都用路由器包裹。我不明白如何使用 HashRouter,我读过它。

标签: reactjs routes react-router react-router-dom protected


【解决方案1】:

您需要将 path="/home" 传递给 GuardedRoute,而不是将其传递给 GuardedRoute 内的 Route

return (
    <div className='App-div'> 
     <Switch>
      <GuardedRoute path='/home' auth={userSignedUp}>
        <Home userIDNumber={userID} setIfSignUp={setIfSignUp} />
      </GuardedRoute>
      {/* remove the exact here so so the route works when something is after the / */}
        <Route path='/signup'>
          <SignUp setUserNumber={setUserID} setIfSignUp={setIfSignUp} />
        </Route>
      </Switch>
    </div>
  );


function GuardedRoute({ children, auth, path }) {
  return (
    <Route
      path={path}
      render={() => {
        return auth ? children : <Redirect to='/signup' />;
      }}
    />
  );
}

class SignUp extends Component {
  constructor(props) {
    super(props);
    //this.signUpProcess = this.signUpProcess.bind(this);
    this.state = {
      stage: 1,
    };
  }

 
  render() {
    return (
  <div className='signUp-div'>
    <Header />
        {/* remove the Router / */}
      <div className='form-div'>
        <Redirect to='/signup/mobile' />
        <Switch>
          <Route  path={'/signup/mobile'} component={MobileNum} />
          <Route  path={'/signup/idnumber'}>
            <IdentNumber setPersonalID={this.props.setUserNumber} />
          </Route>
          <Route  path={'/signup/password'}>
            <CreatePass
              setIfSignUp={this.props.setIfSignUp}
              pathsArray={pathArray}
            />
          </Route>
        </Switch>
      </div>

  </div>
);

} }

在 CreatePass 组件中

const handleClickButton = () => {
    if (passValue.length === 4 && confirmPass.length === 0) {
      textInput.current.focus();
    } else if (passValue === confirmPass && shouldUpdateMessage) {
      // booleanValue();
      //pathsArray.splice(0, pathsArray.length);
      //console.log(pathsArray);
      setIfSignUp(true);
      history.push('/home'); // uncomment this line
    }
  };

【讨论】:

  • 它不起作用,这样什么都不会发生。你真的在我的 CodeSandbox 中尝试过吗?
  • codesandbox.io/s/goofy-fast-9ncv4 这是我改变了一些东西的工作,我将在 anwser 中添加它
  • 显然你错了,在这里查看答案 - stackoverflow.com/questions/68428256/…。如果没有RouterSignup.2 中,它将无法工作)在您的codesanbox 中按退出表单Home 将转移到一个只有css 的空组件。我在我的问题中写了这个。你在代码沙箱中究竟做了什么?我没有看到任何变化。
  • 在没有&lt;Router history={history}&gt; 的情况下与BrowserRouter 相同,但必须在SignUp.jsx 和Index.js 中都有
  • 好吧,我有一个文件没有保存抱歉:codesandbox.io/s/goofy-fast-9ncv4 它适用于我没有注册中的路由器。
猜你喜欢
  • 2020-12-19
  • 1970-01-01
  • 2021-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多