简体   繁体   English

在页面刷新之前反应路线不起作用

[英]React routes not working until page refresh

import React, { useEffect, useState } from 'react';
import { Container, AppBar, Typography, Grow, Grid, useTheme } from '@material-ui/core';
import { useDispatch } from 'react-redux';
import { BrowserRouter, Router, Route, Switch} from 'react-router-dom'

import { getPosts } from './actions/posts'
import Form from './components/Form/Form'
import useStyles from './styles';
import Navbar from './components/Navbar/Navbar'
import Home from './components/Home'
import Auth from './components/Auth'

const App = () => {

 
  return (
    <Container maxwidh='lg'>
      <Navbar />
      <Grow in>
        <Container>
        <div className="content">
        <BrowserRouter>
          <Switch> 
          <Route path="/" exact component={Home} />
          <Route path="/form" exact component={Form} />
          <Route path="/auth" exact component={Auth} />
          </Switch>
        </BrowserRouter>
      </div>
        </Container>
      </Grow>
    </Container>
   );
}



export default App;

This is the Nav bar and above is the routes.这是导航栏,上面是路线。 I'm using express for back end and am not sure if that is playing a roll or not.我在后端使用 express,但不确定它是否正常运行。

import React from 'react';
import { AppBar, Typography } from '@material-ui/core';
import { Link } from 'react-router-dom';
import {BrowserRouter as Router} from "react-router-dom";
import { Toolbar, Avatar, Button } from '@material-ui/core';

import store from '../../images/store.jpg'
import useStyles from './styles';

const Navbar = () => {

    const classes = useStyles();

    return ( 
        <AppBar className = {classes.appBar} position='static' color='inheret'>
            <div className= {classes.brandContainer}>
                <Typography to='/' className = {classes.heading} variant='h2' align='center'>Store</Typography>
                <img className = {classes.image} src={store} alt="store" height='220' width='300' align='center' />
            </div>
            <div >
                <Router>
                <Button component={Link} to="/" variant="contained" color="primary">Home</Button>
                <Button component={Link} to="/form" variant="contained" color="primary">Create Store</Button>
                <Button component={Link} to="/auth" variant="contained" color="primary">Sign Up</Button>
                <Button component={Link} to="/login" variant="contained" color="primary">Login</Button>
                </Router>
            </div>
        
      </AppBar>
     );
}
 
export default Navbar;

When I click the on the links in the browser, the url changes to the correct route but nothing happens to the page.当我单击浏览器中的链接时,url 更改为正确的路由,但页面没有任何反应。 When I refresh the after clicking the correct page loads.当我点击正确的页面加载后刷新。 It does not work without refreshing though.但是,如果不刷新它就无法工作。 Seems simple but not sure what is wrong.看起来很简单,但不确定哪里出了问题。

It is not working because Navbar is outside BrowserRouter .它不起作用,因为 Navbar 在BrowserRouter之外。 Move BrowserRouter in your App component and Remove Router from Navbar:在您的 App 组件中移动BrowserRouter并从导航栏中删除Router

 return (

  <Container maxwidh='lg'>
    <BrowserRouter>
      <Navbar />
      <Grow in>
        <Container>
          <div className="content">
           <Switch> 
             <Route path="/" exact component={Home} />
             <Route path="/form" exact component={Form} />
             <Route path="/auth" exact component={Auth} />
           </Switch>
          </div>
        </Container>
      </Grow>
   </BrowserRouter>
 </Container>

   );

Alternatively you can do like this.或者你可以这样做。 your App component must be Wrapped with BrowserRouter.您的 App 组件必须使用 BrowserRouter 进行包装。

  ReactDOM.render(
  <Router history={history}>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </Router>,
  document.getElementById("root")
);

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

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