【发布时间】: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