gpt4 book ai didi

reactjs - 连接路由器类型错误: Cannot read property 'dispatch' of undefined

转载 作者:行者123 更新时间:2023-12-03 13:18:42 26 4
gpt4 key购买 nike

按照第 3 部分的教程尝试使用 React/Redux:https://tighten.co/blog/react-101-routing-and-auth .

第 1 部分和第 2 部分非常棒,直到第 3 部分我遇到了标题中的错误。我很确定到目前为止我一直在很好地遵循本教程。

非常感谢任何帮助。提前致谢。

Error Info

The above error occurred in the <ConnectedRouter> component:
index.js:1446
in ConnectedRouter (at App.js:39)
in App (created by Context.Consumer)
in Connect(App) (at src/index.js:11)
in Provider (at src/index.js:10)

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './containers/App';
import { Provider } from 'react-redux';
import { configureStore } from './store/configureStore';

const store = configureStore();

ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('app')
)

App.js

import React from 'react';
import { ConnectedRouter } from 'react-router-redux';
import { Route, Redirect } from 'react-router-dom'
import { history } from './../store/configureStore';
import { connect } from 'react-redux';

import Header from '../containers/Header';
import Home from '../containers/Home';
import Signup from '../containers/Signup';
import Login from '../containers/Login';
import Favorites from '../containers/Favorites';

const PrivateRoute = ({component: Component, authenticated, ...props}) => {
return (
<Route
{...props}
render={(props) => authenticated === true
? <Component {...props} />
: <Redirect to={{pathname: '/login', state: {from: props.location}}} />}
/>
);
};

const PublicRoute = ({component: Component, authenticated, ...props}) => {
return (
<Route
{...props}
render={(props) => authenticated === false
? <Component {...props} />
: <Redirect to='/favorites' />}
/>
);
};


class App extends React.Component {
render() {
return (
<ConnectedRouter history={history}>
<div>
<Header />

<div className="container">
<Route exact path="/" component={ Home }/>
<PublicRoute authenticated={this.props.authenticated } path="/signup" component={ Signup } />
<PublicRoute authenticated={this.props.authenticated } path="/login" component={ Login } />
<PrivateRoute authenticated={this.props.authenticated } path="/favorites" component={ Favorites } />
</div>
</div>
</ConnectedRouter>
);
}
}

const mapStateToProps = (state) => {
return { authenticated: state.auth.authenticated };
};

export default connect(mapStateToProps)(App);

ConfigureStore.js

import { createStore, compose, applyMiddleware } from 'redux';
import ReduxPromise from 'redux-promise';
import rootReducer from '../reducers';
import createHistory from 'history/createBrowserHistory';
import { routerMiddleware } from 'react-router-redux';

export const history = createHistory();

export function configureStore(initialState) {
const store = createStore(
rootReducer,
initialState,
compose (
applyMiddleware(ReduxPromise, routerMiddleware(history)),
window.devToolsExtension ? window.devToolsExtension() : f => f
)
//window.devToolsExtension ? window.devToolsExtension() : undefined
);

if (module.hot) {
// Enable Webpack hot module replacement for reducers
module.hot.accept('../reducers', () => {
const nextRootReducer = require('../reducers').default;
store.replaceReducer(nextRootReducer);
});
}

return store;
}

最佳答案

关于reactjs - 连接路由器类型错误: Cannot read property 'dispatch' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54402637/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com