【问题标题】:Browser rendering an empty Page浏览器呈现一个空页面
【发布时间】:2021-12-30 10:34:09
【问题描述】:

我正在使用引导程序来帮助我创建登录表单。但是在渲染这个登录组件时,我得到一个空的浏览器,但是当我渲染没有表单组和表单控件的其他组件时,我得到了浏览器中的内容。

import React from 'react'
import { Button} from 'react-bootstrap';
import { Form } from 'react-bootstrap';
import GoogleButton from 'react-google-button';
import { Link } from 'react-router-dom';


const SignIn = () => {

    return (
        <>
      
            <div className="p-4 box">
                <h2 className="mb-3">Bonnie Electronics</h2>
                <Form>

                    <Form.Group className="mb-3" controlId="formBasicEmail">
                        <Form.Control type='email' placeholder="email address" />
                    </Form.Group>

                    <Form.Group className="mb-3" controlId="formBasicPassword">
                        <Form.Control type='password' placeholder="password" />
                    </Form.Group>

                    <div className="d-grid gap-2">
                         <Button type="submit" variant="primary">Login</Button>
                    </div>

                </Form>

                <hr />

                 <div>
                     <GoogleButton type="dark" className="g-button" />
                 </div>
                
            </div>
            <div className="p-4 box mt-3 text-center">
                Don't Have an Account? <Link to='/signup'>SignUp</Link>`enter code here`
            </div>

       </>
       
    )   
}

export default SignIn;

【问题讨论】:

    标签: javascript reactjs react-bootstrap


    【解决方案1】:

    您需要在 inside 路由上下文中呈现此组件 SignIn,因为您正在使用来自 react react-router-dom

    Link >

    你正在做类似的事情:

    <SignIn /> // <-- This can't be outside router!
    
    <Router>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/whatever" element={<YourComp/>} />
      </Routes>
    </Router>
    

    您应该将它移动到路由上下文中,以便路由器知道并正确管理路由。

    <Router>
      <SignIn/>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/whatever" element={<YourComp/>} />
      </Routes>
    </Router>
    

    我创建了 Demo

    https://codesandbox.io/s/sweet-morning-0pt00?file=/src/SignIn.js

    您还可以在此处阅读更多参考资料:

    Error: useHref() may be used only in the context of a <Router> component. It works when I directly put the url as localhost:3000/experiences

    【讨论】:

    • 按照您的建议工作得很好。非常感谢我一直在启动项目。认为我一直在犯错误,我一直在使用 Link 并且不知道组件应该包含在 routs 和 Routers 中。堆栈溢出是家
    • 没问题,伙计 :) 每天我们都在学习新事物,最好的办法是意识到我们一直在做的不好的事情,将其修复以备将来使用
    猜你喜欢
    • 2017-05-30
    • 1970-01-01
    • 2013-07-04
    • 2013-03-22
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    • 2022-07-11
    • 1970-01-01
    相关资源
    最近更新 更多