history
小道具をApp
からナビゲーションコンポーネントに渡します。私はそうしようとするとき 、私は次のエラーメッセージが表示されます。コンポーネントにhistory propを渡すことができません
失敗しましたプロップタイプ:Navigation
に必要とされる小道具history
がマークされているが、その値はundefined
です。
この問題を解決するにはどうすればよいですか?
App.js:
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
const App = props => (
<Router>
<MainLayout {...props}>
<Switch>
<Route exact name="index" path="/" component={Index}/>
<Route component={NotFound}/>
</Switch>
</MainLayout>
</Router>
);
MainLayout.js:
import React from "react";
import PropTypes from "prop-types";
import Navigation from "../../components/Navigation/Navigation";
const MainLayout = props => {
const { children, authenticated, history } = props;
return (
<div>
<Navigation authenticated={authenticated} history={history} />
{children}
</div>
);
};
MainLayout.PropTypes = {
children: PropTypes.node.isRequired,
authenticated: PropTypes.bool.isRequired,
history: PropTypes.object.isRequired,
};
export default MainLayout;
は '' MainLayout'の内部値を持つhistory'ていますか?あなたのエラーは、 'Navigation'がそれを取得しているときに' undefined'であることを示唆しています。また、 'App'は'歴史 'の支柱を受けているようだ。その小道具を追跡し、それが通過する前にそれが定義されていることを確認してください。 –
'MainLayout'の中の' history'の値は 'undefined'ですが、 'App'の中には値が割り当てられています –