简体   繁体   English

从React-Bootstrap Navbar搜索路由到搜索组件

[英]Route to Search Component from React-Bootstrap Navbar Search

I have a react navbar in my main component and on there I have a search bar. 我的主要组件中有一个react导航栏,在那上面有一个搜索栏。 I want to be able to have users type text into the search box and then for it to route to a search component which returns the results. 我希望能够让用户在搜索框中键入文本,然后路由到返回结果的搜索组件。

The problem is that the main component doesn't have the props.history to enable me to push the route. 问题在于主要组件没有props.history使我能够推动路线。 I understand that this is because the props are part of the router component which it won't be able to access. 我知道这是因为道具是路由器组件的一部分,它将无法访问。

I have tried creating a separate component for the search part of the navbar, but it still doesn't have the props. 我尝试为导航栏的搜索部分创建一个单独的组件,但它仍然没有道具。 I will consider writing the main component to perform all the searches, but I want to keep it separate from the main code really. 我将考虑编写主要组件以执行所有搜索,但是我确实希望将其与主要代码分开。 Is there another way to do this. 还有另一种方法可以做到这一点。

My code: 我的代码:

index.js index.js

import React from "react";
import ReactDOM from "react-dom";
import Main from "./Main";
import "./index.css";


ReactDOM.render(
  <Main/>,
  document.getElementById("root")
);

main.js main.js

import React, { Component } from "react";
import {
  Route,
  NavLink,
  HashRouter
} from "react-router-dom";

import Home from "./home";
import FindBooks from "./findbooks";

import {
  Container,
  Row,
  Col,
  Navbar,
  Nav,
  FormControl,
  Form,
  Button
} from 'react-bootstrap';

class Main extends Component {


  constructor(props) {
    super(props);
    this.state = ({
            });
  }


  findBooks = () => {
    //Route to my FindBooks component
  }

  render() {
    //alert(Auth.getTokenExpiry());
    //alert(this.state.loggedin);
    if(this.state.loggedin) {
      return (
        <HashRouter>
          <Container>
            <Row>
              <Col md={4}>
              <Navbar bg="dark" variant="dark">
                <Nav className="mr-auto">
                  <NavLink to="/">Home</NavLink>
                </Nav>
                <Form inline>
                  <FormControl type="text" placeholder="Search" className="mr-sm-2" onChange={this.changeSearchText} value={this.state.searchtext} onClick={this.findBooks}/>
                  <Button variant="outline-primary">Search</Button>
                </Form>
              </Navbar>
              </Col>
            </Row>
          </div>
            <Row>
              <Col></Col>
              <Col md={10}>
                <div style={divStyle}></div>
                <Route exact path="/" component = {Home}/>
                <Route path="/findbooks" render={props => (<FindBooks {...props} searchstring={this.state.searchtext} searchterms {"bytitle"} />)}/>
              </Col>
            </Row>
          </Container>
        </HashRouter>
      );
  }

}

export default Main;

The findBooks method is where I want to force it to route to the FindBooks component. 我想强制将findBooks方法路由到FindBooks组件。

Is something like this what you're looking for? 您正在寻找类似这样的东西吗?

编辑React-带有路由的NavBar搜索

For the sake of "archival": 为了“档案”:

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

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