简体   繁体   English

使用历史推送的Reactjs路由,但只能更改URL的视图

[英]Reactjs Routing with history push but doent change the view only the URL

I'm working with a menu and this have a ListGroupItem and try to redirect another component with history push but only change in the url! 我正在使用菜单,并且它具有ListGroupItem并尝试通过历史记录推送来重定向另一个组件,但只能更改url! This url works but it doesn't change the view, I have to press enter to make this work. 该网址有效,但它不会改变视图,我必须按Enter才能使其正常工作。

I'm new with react, especially with the router, and I'm feeling lost with this. 我是React的新手,尤其是路由器,对此我感到迷茫。 Thanks for any help. 谢谢你的帮助。

This is my App.js 这是我的App.js

import React, { Component } from 'react';
import './css/App.css';
import { BrowserRouter, Route, Link } from 'react-router-dom';
import Prueba from './Prueba';
import Menu from './header/Menu';

class App extends Component {

  toggle(menu) {
    if (this.state.collapse == menu){
      this.setState({ collapse: false }); 
    }else {
      this.setState({ collapse: menu });
    }
  }

  render() {
    return (
      <BrowserRouter>
       <Menu/>
       <Prueba/>
      </BrowserRouter>
    );
  }
}

export default App;

This is my another component with name Prueba.js 这是我的另一个名为Prueba.js的组件

import React, { Component } from 'react';
import './css/App.css';
import { BrowserRouter, Route,Link} from 'react-router-dom';
import { ListGroup, ListGroupItem,Collapse, Container, CardBody, Card, Row, Col} from 'reactstrap';
import Pais from './UbicacionGeneral/Pais';
import { withRouter } from 'react-router';

class Prueba extends React.Component{

  constructor(props) {
    super(props);
    this.toggle = this.toggle.bind(this);
    this.state = { collapse: false };
    this.routeChange = this.routeChange.bind(this);    
  }

  routeChange() {
    this.props.history.push('Pais');
  }

  componentDidUpdate(){

  }

  toggle(menu) {

    if (this.state.collapse == menu){
      this.setState({ collapse: false }); 
    }else {
      this.setState({ collapse: menu });
    }   
  }

  render() {
    return (
  <BrowserRouter>

    <div className="App text-center">
      <Row>
        <Col  md="2">
          <ListGroup className="List-Principal">
            <ListGroupItem className="List-Principal-Item " onClick={() => this.toggle("ubicacion")} > Ubicacion General </ListGroupItem>

             <Collapse isOpen={this.state.collapse == "ubicacion"}> 
              <ListGroup>
                <ListGroupItem  onClick={this.routeChange} > Pais </ListGroupItem>
                <ListGroupItem  > Estado </ListGroupItem>
                <ListGroupItem  > Ciudad </ListGroupItem>
              </ListGroup>
            </Collapse>

          <ListGroupItem  className="List-Principal-Item tex-center" 

          onClick={() => this.toggle("almacen")} > Almacen </ListGroupItem>
                <Collapse  isOpen={this.state.collapse == "almacen"}>
                  <ListGroup>
                    <ListGroupItem  > Crear - Modificar  </ListGroupItem>
                    <ListGroupItem  > Verificar Stock   </ListGroupItem>
                    <ListGroupItem  > Movimientos  </ListGroupItem>
                  </ListGroup>
                </Collapse>
            </ListGroup>

          </Col>

          <Col  md="10">   
            <Container>
              <Route path="/Pais" component={Pais} />
            </Container>
          </Col>        
        </Row>
      </div>
     </BrowserRouter>
    );
  }
}

export default withRouter(Prueba);

Finally this is the component I want to see with the history push, named Pais. 最后,这是我想通过历史记录推送看到的名为Pais的组件。

Pais.js Pais.js

import React, { Component } from 'react';
import { ListGroup, ListGroupItem,Collapse, Container, CardBody, Card, Row, Col} from 'reactstrap';
import { Alert } from 'reactstrap';
import { withRouter } from 'react-router-dom';


class Pais extends Component {

  render() {

    return (
      <div>
        <Alert color="primary">
          This is a primary alert — check it out!
        </Alert>
        <Alert color="secondary">
          This is a secondary alert — check it out!
        </Alert>
        <Alert color="success">
          This is a success alert — check it out!
        </Alert>
        <Alert color="danger">
          This is a danger alert — check it out!
        </Alert>
        <Alert color="warning">
          This is a warning alert — check it out!
        </Alert>
        <Alert color="info">
          This is a info alert — check it out!
        </Alert>
        <Alert color="light">
          This is a light alert — check it out!
        </Alert>
        <Alert color="dark">
          This is a dark alert — check it out!
        </Alert>        
      </div>
    );
  } 
}

export default withRouter (Pais);

There are several parts that are incorrect. 有几个部分不正确。 First of all, you shouldn't use BrowserRouter in Prueba.js. 首先,您不应该在Prueba.js中使用BrowserRouter。 There should be only one BrowserRouter. 应该只有一个BrowserRouter。 Also, you either put Route inside BrowserRouter or Switch. 另外,您可以将Route放在BrowserRouter或Switch中。 As you didn't configure routes properly using BrowserRouter, Switch and Route, url changes but that doesn't reflect to your React App. 由于您没有使用BrowserRouter,Switch和Route正确配置路由,因此url发生了变化,但这并没有反映到您的React App中。

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

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