【问题标题】:Link of react-router-dom updates the URL without rendering the componentreact-router-dom 的链接更新 URL 而不渲染组件
【发布时间】:2021-07-20 04:18:08
【问题描述】:

我正在使用 react-router-dom 链接组件来管理我的导航,但它不起作用。 链接更改 URL 但不呈现组件。 这是一个描述我的问题的简单应用程序。我正在尝试使用 My App 标头作为回家链接:

    import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
import React from 'react';

const Navigation = () => {
  return (
    <div>
      <Link to="/events">
        <h1>My Application</h1>
      </Link>
    </div>
  );
};
const ConnectedNavigation = connect((state) => state)(Navigation);

export default ConnectedNavigation;

App.jsx 代码:

import React from 'react';
import { Provider } from 'react-redux';
import { store } from '../store/index';
import ConnectedDashboard from './Dashboard';
import ConnectedEventDetails from './EventDetails';
import { Router, Route } from 'react-router-dom';
import { history } from '../store/history';
import ConnectedNavigation from './Navigation';

export const Main = () => (
  <Router history={history}>
    <Provider store={store}>
      <div>
        <ConnectedNavigation />
        <Route exact path="/events" render={() => <ConnectedDashboard />} />
        <Route
          exact
          path="/event/:id"
          //match prop is necessary in order to determine which event
          //we are looking for
          render={({ match }) => <ConnectedEventDetails match={match} />}
        />
      </div>
    </Provider>
  </Router>
);

如您所见,我在路线上添加了准确信息以避免任何混淆

【问题讨论】:

  • 你需要用 Switch 组件包装你的 Route 组件
  • 试过但没用

标签: reactjs react-redux react-router react-router-dom


【解决方案1】:

这个问题来自这一行:

import { Router, Route } from 'react-router-dom';

正确的导入是:

import { BrowserRouter as Router, Route } from 'react-router-dom';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 2019-01-15
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 2020-08-16
    相关资源
    最近更新 更多