以前也有人问过类似的问题,但这似乎是特定于connected-react-router
. 我可以使用Router
or BrowserRouter
fromreact-router(-dom)
并且没有问题,但我想合并 Redux 因此使用这个包。
应用程序.js
import React, { Component } from "react";
import { Provider } from "react-redux";
import { ConnectedRouter } from "connected-react-router";
// import { Router, Route, Switch } from "react-router-dom";
import { Route, Switch } from "react-router";
// Routes
// import routes from "./js/routes";
// Components
import PimberlyLogin from "./js/containers/PimberlyLogin";
import CognitoLogin from "./js/containers/CognitoLogin";
import CognitoChangePassword from "./js/views/CognitoChangePassword";
import ListGroups from "./js/containers/ListGroups";
import VerificationCode from "./js/views/VerificationCode";
// Auth component
import { requireAuthentication } from "./js/components/AuthenticatedComponent";
// Styles
import "./css/app.css";
export default class App extends Component {
constructor(props) {
super(props);
}
render() {
return (
<Provider store={this.props.store}>
<ConnectedRouter history={this.props.history}>
<Switch>
<Route
path="/"
component={PimberlyLogin}
exact={true}/>
<Route
path="/cognito/login"
component={CognitoLogin}
exact={true}/>
<Route
path="/cognito/changePassword"
component={CognitoChangePassword}
exact={true}/>
<Route
path="/groups"
component={requireAuthentication(ListGroups)}
exact={true}/>
<Route
path="/cognito/verificationCode"
component={VerificationCode}
exact={true}/>
</Switch>
</ConnectedRouter>
</Provider>
);
}
}
相对简单,类似于NPM 页面上connected-react-router
的示例。
即使我查看他们 GitHub 上的基本示例,也有类似的编码模式。
这些是我得到的错误:
第二个错误似乎来自我的高阶组件,所以这是代码:
PimberlyLogin.js
import { connect } from "react-redux";
import { withRouter } from "react-router-dom";
import { bindActionCreators } from "redux";
import PimberlyLoginComponent from "../views/PimberlyLogin";
import { setUsersPersist } from "../actions/user";
import { setGroupsPersist } from "../actions/group";
const mapStateToProps = (state, props) => {
return {
users: state.user,
groups: state.group
};
};
const mapDispatchToProps = (dispatch) => {
return bindActionCreators({
setUsersPersist: () => setUsersPersist(),
setGroupsPersist: () => setGroupsPersist()
});
};
export default withRouter(connect(
mapStateToProps,
mapDispatchToProps
)(PimberlyLoginComponent));
编辑
我玩了抓取<Switch>
和<Route>
从不同的包(react-router
& react-router-dom
)。目前我正在做:
import { Switch, Route } from "react-router";
...并且我得到:You should not use <Route> outside a <Router>.
如果我做:
import { Switch } from "react-router-dom";
import { Route } from "react-router";
我得到:You should not use <Switch> outside a <Router>.
这是否可能与<Route>
fromreact-router
播放方式有关connected-react-router
?
版本:
react-router
@ v4.3.1react-router-dom
@ v4.4.0-betaconnected-react-router
@v6.0.0react
@ v16.5.2