2
私はコード分割で反応ルータを使用しています。 AuthenticatedRoute
のような動的ルートはうまく機能しないようです。コード分割チャンクを使用した動的ルートは機能していないようです
ユーザーがログに、About
ルートに行くと戻って(ページを更新せずに)About
ルートに入った場合About
経路コード分割チャンクを交換/更新し、それを取得していないため、何も表示されませんコンポーネントがインポートされたAbout
がありません。
コード:
ルート
import React from 'react';
import { Route } from 'react-router-dom'
import asyncComponent from "./AsyncComponent";
import AuthenticatedRoute from "./AuthenticatedRoute";
const AsyncHome = asyncComponent(() => import("../../containers/Home/Home"));
const AsyncAbout = asyncComponent(() => import("../../containers/About/About"));
const AsyncLogin = asyncComponent(() => import("../../containers/Login/Login"));
const Routes =() => (
<main>
<Route exact path="/" component={AsyncHome} />
<AuthenticatedRoute exact path="/about" component={AsyncAbout} />
<Route exact path="/login" component={AsyncLogin} />
</main>
)
export default Routes
AuthenticatedRoute
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { Route, Redirect } from 'react-router-dom';
class AuthenticatedRoute extends Component {
render() {
const { props } = this;
const { component: C, user, ...newProps } = props;
const renderComponent = (routeProps) => {
return user ? <C {...routeProps} /> : <Redirect to="/login" />;
}
return <Route {...newProps} render={renderComponent} />
}
}
const mapStateToProps = state => ({
user: state.auth.user
})
const mapDispatchToProps = dispatch => bindActionCreators({
}, dispatch)
export default connect(
mapStateToProps,
mapDispatchToProps,
)(AuthenticatedRoute)
問題がチャンクの発生と関連しているように見えます。ユーザがログインせずにAbout
ルートチャンクが作成された場合、そのチャンクにはAbout
コンポーネントがありません。ログイン後にページを更新すると、About
ルートチャンクにはAbout
のコンポーネントがあり、すべて正常に動作します。
ユーザーがログインしていないときに意図したとおりに機能しています。ユーザーがログインしてもAboutルートのコード分割チャンクが表示されます同じで、Aboutコンポーネントが表示されません。 – JPHorta