Reactjs路由不导航

发布于 2025-01-10 13:30:34 字数 2179 浏览 0 评论 0原文

我目前正在开发一个反应应用程序,并且路由有问题。我之前已经像这样设置了 React 应用程序及其路由,但是当尝试路由到“详细信息”组件时,只有 url 发生变化,但该组件未加载。多一双眼睛就很高兴看到我所错过的东西。我将路由设置为:

  1. index.js:
    import React from 'react';
    import ReactDOM from 'react-dom';
    import App from './App';
    import { Router } from "react-router-dom";
    import { createBrowserHistory } from "history";
    
    const history = createBrowserHistory();
    
    const render = () => {
        ReactDOM.render(
            <Router history={history}>
                <App />
            </Router>,
            document.getElementById('root'),
        );
    };
    
    render();
  1. App.js:
    import React from 'react';
    import {Route, Switch} from 'react-router-dom';
    import { BreweryPage } from './route.js';
    
    function App() {
      return (
          <>
            <Switch>
              <Route exact path="/" component={BreweryPage}/>
            </Switch>
          </>
      );
    }
    
    export default App;
  1. route.js
    import React from 'react';
    import PropTypes from 'prop-types';
    import { Route, Switch, withRouter } from 'react-router-dom';
    import { BreweryPage, BreweryDetailPage } from './pages';
    import {AppContainer} from "../../common/header";
    
    const BreweryHomePage = ({ match }) => (
        <AppContainer>
            <Switch>
                <Route exact path={`${match.url}`} component={BreweryPage} />
                <Route exact path={`${match.url}/details/:breweryid`} component={BreweryDetailPage} />
            </Switch>
        </AppContainer>
    );
    
    BreweryHomePage.propTypes = {
        match: PropTypes.shape({
            url: PropTypes.string,
        }),
    };
    
    BreweryHomePage.defaultProps = {
        match: {
            url: '',
        },
    };
    
    export default withRouter(BreweryHomePage);

加载根“/”路径组件,但在使用 history.push( 路由时无法获取要渲染的详细信息组件组件路径)使用 const History = useHistory();

I currently have a react app I am working on and the routing is buggy. I have set up react applications and their routings like this before but when trying to route to the "details" component, only the url changes, but the component does not load. An extra pair of eyes would be nice to see what I'm missing. I have the routes set up as:

  1. index.js:
    import React from 'react';
    import ReactDOM from 'react-dom';
    import App from './App';
    import { Router } from "react-router-dom";
    import { createBrowserHistory } from "history";
    
    const history = createBrowserHistory();
    
    const render = () => {
        ReactDOM.render(
            <Router history={history}>
                <App />
            </Router>,
            document.getElementById('root'),
        );
    };
    
    render();
  1. App.js:
    import React from 'react';
    import {Route, Switch} from 'react-router-dom';
    import { BreweryPage } from './route.js';
    
    function App() {
      return (
          <>
            <Switch>
              <Route exact path="/" component={BreweryPage}/>
            </Switch>
          </>
      );
    }
    
    export default App;
  1. route.js
    import React from 'react';
    import PropTypes from 'prop-types';
    import { Route, Switch, withRouter } from 'react-router-dom';
    import { BreweryPage, BreweryDetailPage } from './pages';
    import {AppContainer} from "../../common/header";
    
    const BreweryHomePage = ({ match }) => (
        <AppContainer>
            <Switch>
                <Route exact path={`${match.url}`} component={BreweryPage} />
                <Route exact path={`${match.url}/details/:breweryid`} component={BreweryDetailPage} />
            </Switch>
        </AppContainer>
    );
    
    BreweryHomePage.propTypes = {
        match: PropTypes.shape({
            url: PropTypes.string,
        }),
    };
    
    BreweryHomePage.defaultProps = {
        match: {
            url: '',
        },
    };
    
    export default withRouter(BreweryHomePage);

The root "/" path component loads, but I can't get the details components component to render when routing with history.push(path) using const history = useHistory();.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人练习一个人 2025-01-17 13:30:34

这样做

1-index.js 应该像这样:

import React from "react"
import ReactDOM from "react-dom"
import App from "./App"
import { BrowserRouter } from "react-router-dom"

const app = (
  <React.StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  <React.StrictMode/>
)

ReactDOM.render(app, document.getElementById("root"))

你必须添加 BrowserRouter 才能使用导航

2-你可以在 App.js 中使用 BreweryPage,如下所示:

import React from 'react'; 
import {Route, Switch} from 'react-router-dom';
import { BreweryPage } from './route.js';

function App() { 
return ( 
<>
<BreweryPage/> 
</> 
); 
}
export default App;

3-route.js

import React from 'react'; 
import PropTypes from 'prop-types'; 
import { Route, Switch, withRouter } from 'react-router-dom'; 
import { BreweryPage, BreweryDetailPage } from './pages'; 
import { createBrowserHistory } from "history";
import {AppContainer} from "../../common/header";
const newHistory = createBrowserHistory();

const BreweryHomePage = (props) => (
<Router history={newHistory}>
<Switch> 
<Route exact path="/" component={BreweryPage} /> 
<Route path="/details/:breweryid" component={BreweryDetailPage} /> 
</Switch>
</Router>
);


export default withRouter(BreweryHomePage);

do it like this

1-index.js should like this:

import React from "react"
import ReactDOM from "react-dom"
import App from "./App"
import { BrowserRouter } from "react-router-dom"

const app = (
  <React.StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  <React.StrictMode/>
)

ReactDOM.render(app, document.getElementById("root"))

you have to add BrowserRouter to be able to use navigation

2- you can use BreweryPage in App.js like this:

import React from 'react'; 
import {Route, Switch} from 'react-router-dom';
import { BreweryPage } from './route.js';

function App() { 
return ( 
<>
<BreweryPage/> 
</> 
); 
}
export default App;

3- route.js

import React from 'react'; 
import PropTypes from 'prop-types'; 
import { Route, Switch, withRouter } from 'react-router-dom'; 
import { BreweryPage, BreweryDetailPage } from './pages'; 
import { createBrowserHistory } from "history";
import {AppContainer} from "../../common/header";
const newHistory = createBrowserHistory();

const BreweryHomePage = (props) => (
<Router history={newHistory}>
<Switch> 
<Route exact path="/" component={BreweryPage} /> 
<Route path="/details/:breweryid" component={BreweryDetailPage} /> 
</Switch>
</Router>
);


export default withRouter(BreweryHomePage);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文