12

以前也有人问过类似的问题,但这似乎是特定于connected-react-router. 我可以使用Routeror BrowserRouterfromreact-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.1
  • react-router-dom@ v4.4.0-beta
  • connected-react-router@v6.0.0
  • react@ v16.5.2
4

2 回答 2

6

我认为这是因为您使用的是 2 个版本react-router(4.3.1 用于核心路由器,4.4.0-beta 用于react-router-dom)。我有同样的问题。我react-router从我的项目中删除(因为它带有react-router-dom)并使用 v4.3.1。在那之后它似乎工作得很好。

所有这些都基于' s 维护者的回复connected-react-router

于 2019-04-26T13:18:13.580 回答
4

利用

"react-router": "^4.3.1"
"react-router-dom": "^4.3.1"
于 2019-05-24T03:25:15.830 回答