简体   繁体   English

在一半页面上反应路由器导航

[英]react router navigation in half page

I am designing a website in which the header is the same for three pages. 我正在设计一个网站,其中三个页面的标题都是相同的。 By default the fist page should have the following components 默认情况下,“拳头”页面应具有以下组件

Header QueryList 标头查询列表

and on click of a button present in Header the QueryList component should change to FindExpert.js. 然后单击标题中存在的按钮,QueryList组件应更改为FindExpert.js。 Along with the change in Page, the URL also changes on the click of a button. 随着页面的更改,单击按钮也将更改URL。

Header FindExpert 标头FindExpert

Since header is present in both the screens and I don't want to call the component again and again in every js file.So, I used ternary operator in the header.js file so that I can route in half of the page. 因为标题在两个屏幕上都存在,并且我不想在每个js文件中一次又一次地调用该组件,所以我在header.js文件中使用了三元运算符,以便可以路由页面的一半。

App.js App.js

import React, { Component } from 'react';
import {BrowserRouter,Route,Router,Switch} from 'react-router-dom';
import {Header} from './Header.js';
import {FindExpert} from './FindExpert.js';

class App extends Component {
  render() {
    return (
      <div>
        <BrowserRouter>
            <Switch>
              <Route exact path="/" component={Header} />
            <Route path="/findexpert" component={FindExpert} />
            </Switch>
        </BrowserRouter>
      </div>
    );
  }
}

export default App;

Header.js Header.js

import React, { Component } from 'react';
import {Constants} from './constants.js';
import {QueryList} from './QueryList.js';
import {FindExpert} from './FindExpert.js';

export class Header extends React.Component{

    constructor(props) {
        super(props);
        this.state = {
       ProfileData: {},
             profileImage: "profile-pic.svg",
       isClicked:false

        };
        this.findExpertClicked = this.findExpertClicked.bind(this);
        this.findExpertPage=this.findExpertPage.bind(this)
    }

          findExpertClicked() {
         console.log("Find expert clicked");
         this.setState({
            isClicked:true
         })
     }

     findExpertPage(){
        this.props.history.push('/findexpert');
     }

    render(){
        return( 
            <div>
                <div className="col-md-12 HeaderBar">
                    <div className="HeaderContent">
                    </div>
                </div>
                <div className="ProfileHeader">
                    <div className="profileImageDiv">       
                        <img src={this.state.profileImage} className="profileImageSrc" alt="Alternate"></img>
                    </div>
                    </div>
              <div className="headerButtons">
                  <div className="ExBtnDiv" onClick={this.findExpertClicked}><span className="ExBtn">Find Expert</span></div>
              </div>   
                </div>
            <div>{(this.state.isClicked===false)?<div><QueryList/></div> :<div>{this.findExpertPage()}</div>}</div>
            </div>
        )
    }
}

And in FindExpert.js, suppose I have a simple Hello world. 在FindExpert.js中,假设我有一个简单的Hello世界。

import React, { Component } from 'react';
import {Header} from './Header.js';
import {Constants} from './constants.js';

export class FindExpert extends React.Component{

    render(){
        return( 
            <div>       
               Hello world
            </div>
        )
    }
}

Through the above code, the QueryList comes below the Header as expected but when clicked on the find expert button, the whole page changes. 通过上面的代码,QueryList按预期位于标题下方,但是单击“查找专家”按钮时,整个页面都会更改。

Since I don't want to call header.js in both the files, is there a way to keep the header constant and change the contents below it as well as change the url when find expert button is clicked. 由于我不想在两个文件中都调用header.js,因此有一种方法可以保持标头恒定并更改其下的内容以及单击“查找专家”按钮时更改url。

You have to create separate components for Header and the content to be shown on the homepage. 您必须为Header和要在主页上显示的内容创建单独的组件。 And directly render the Header component inside App.js 并直接在App.js中呈现Header组件

This way Header component will always be rendered and only the page content would change according to the current route. 这样,Header组件将始终呈现,并且仅页面内容会根据当前路由更改。

For example: 例如:

import React, { Component } from 'react';
import {BrowserRouter,Route,Router,Switch} from 'react-router-dom';
import {Header} from './Header.js';
import HomePage from './HomePage.js';
import {FindExpert} from './FindExpert.js';

class App extends Component {
  render() {
    return (
      <div>
        <Header />
        <BrowserRouter>
            <Switch>
              <Route exact path="/" component={HomePage} />
              <Route path="/findexpert" component={FindExpert} />
            </Switch>
        </BrowserRouter>
     </div>
    );
  }
}

export default App;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM