react-router-dom 的链接在不渲染组件的情况下更新 URL

时间:2021-04-26 19:21:33

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

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

    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>
);

正如你所看到的,我在我的路线上添加了精确以避免任何混淆

1 个答案:

答案 0 :(得分:2)

这个问题来自这一行:

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

正确的导入是:

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