【问题标题】:Component rendering on same page instead of a new page in React JS在 React JS 中的同一页面而不是新页面上呈现组件
【发布时间】:2019-07-06 03:01:10
【问题描述】:

我是 React JS 的新手,实际上只用了几天。我目前正在尝试处理不同页面之间的导航。

基本上,我目前想要做的是,当我在 SigninForm 屏幕上单击“登录”时,我希望它导航到一个新的“SupervisorDashboard”页面,该页面当前有虚拟 h1 文本说“这是一个测试”。

目前,当我单击“登录”按钮时,“这是一个测试”会在按钮下方呈现,而不是在新页面上呈现。 url 更新,但 h1 文本显示在同一页面上而不是新页面上。

我在 StackOverflow 以及其他网站上尝试了很多解决方案,但似乎都没有奏效,这让我觉得我犯了一个小错误。谁能看看我的 SignInForm.js 代码并找出问题所在?:

import React, { Component } from 'react';
import { BrowserRouter as Router, Link, Route} from 'react-router-dom';
import SupervisorDashboard from '../pages/SupervisorDashboard';


class SignInForm extends Component {



render() {

    return (

      <Router>

        <div className="FormCenter">

        <form className="FormFields" onSubmit={this.handleSubmit}>

          <div className="FormField">

            <label className="FormField__Label" htmlFor="cnic">CNIC</label>

            <input type="text" id="cnic" className="FormField__Input" placeholder="Enter your CNIC without dashes" name="cnic"/>

          </div>

          <div className="FormField">

          <Link to={{
              pathname: '/SupervisorDashboard'
            }} className="FormField__Button mr-20">Sign In</Link>

            </div>


        </form>

        <Route exact path="/SupervisorDashboard" component={SupervisorDashboard}>

        </Route>


      </div>


      </Router>
    );
}
}

export default SignInForm;

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    基本上,当 path 属性适合时,Route 元素总是会被渲染。

    因此,只要 url 为“/SupervisorDashboard”,您就会呈现您的 SupervisorDashboard。通过使用一个开关和多个 Render 元素,您可以根据您的 url 在所有渲染元素之间切换

    我更正了您的代码。这是伪代码,我目前无法测试此代码,但您应该看到我所做的更改:

    import React, { Component } from 'react';
    import { BrowserRouter as Router, Link, Route} from 'react-router-dom';
    import SupervisorDashboard from '../pages/SupervisorDashboard';
    
    class SignInForm extends Component {
    
        render() {
            return (
                <Router>
                    <Switch>
                    <Route path="/" exact render={() => {
                        return (
                            <div className="FormCenter">
                                <form className="FormFields" onSubmit={this.handleSubmit}>
                                    <div className="FormField">
                                        <label className="FormField__Label" htmlFor="cnic">CNIC</label>
                                        <input type="text" id="cnic" className="FormField__Input" placeholder="Enter your CNIC without dashes" name="cnic"/>
                                    </div>
                                    <div className="FormField">
                                        <Link to={{
                                            pathname: '/SupervisorDashboard'
                                        }} className="FormField__Button mr-20">Sign In</Link>
    
                                    </div>
                                </form>
                            </div>
                        );
                    }}/>
                    <Route exact path="/SupervisorDashboard" component={SupervisorDashboard} />
                    </Switch>
                </Router>
            );
        }
    }
    
    export default SignInForm;
    

    【讨论】:

    • 您的代码运行良好(Switch 缺少导入除外),您的解释对 React JS 新手非常有帮助,将您的答案标记为正确。
    猜你喜欢
    • 2018-01-04
    • 2021-12-09
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 2019-03-26
    • 2015-11-24
    • 2020-08-24
    相关资源
    最近更新 更多