无法将 history 道具传递给组件
Cannot pass down history prop to Component
我想将 history
属性从 App
向下传递给导航组件。
当我尝试这样做时,我收到以下错误消息:
失败的道具类型:道具history
在Navigation
中被标记为必需,但其值为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;
解决方案 #1:
如果您只是将 <MainLayout />
转换为呈现的 <Route />
,您将可以访问历史对象。
<Route render={(props) =>
<MainLayout {...props}>
<Switch>
<Route exact name="index" path="/" component={Index}/>
<Route component={NotFound}/>
</Switch>
</MainLayout>
}/>
https://github.com/ReactTraining/react-router/blob/master/packages/react-router/modules/Route.js
<App />
无法访问历史作为道具,所以这永远不会做你想做的事 <MainLayout {...props}>
解决方案 #2
您还可以在您的应用程序中将历史对象作为单个导出模块引用,并在您的应用程序中引用 React 路由器和任何其他组件/javascript 文件。
import { Router, Switch, Route } from 'react-router-dom';
import history from './history';
const App = props => (
<Router history={history}>
<MainLayout history={history} {...props}>
<Switch>
<Route exact name="index" path="/" component={Index}/>
<Route component={NotFound}/>
</Switch>
</MainLayout>
</Router>
);
(history.js)
import createBrowserHistory from 'history/createBrowserHistory';
export default createBrowserHistory();
对于 react-router-dom v4
为了获得 Router 组件的属性,我使用了 withRouter,我想下面的更改应该有效,
export default wihtRouter(MainLayout);
这应该可以在 MainLayout
中使用 props.history
我想将 history
属性从 App
向下传递给导航组件。
当我尝试这样做时,我收到以下错误消息:
失败的道具类型:道具history
在Navigation
中被标记为必需,但其值为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;
解决方案 #1:
如果您只是将 <MainLayout />
转换为呈现的 <Route />
,您将可以访问历史对象。
<Route render={(props) =>
<MainLayout {...props}>
<Switch>
<Route exact name="index" path="/" component={Index}/>
<Route component={NotFound}/>
</Switch>
</MainLayout>
}/>
https://github.com/ReactTraining/react-router/blob/master/packages/react-router/modules/Route.js
<App />
无法访问历史作为道具,所以这永远不会做你想做的事 <MainLayout {...props}>
解决方案 #2
您还可以在您的应用程序中将历史对象作为单个导出模块引用,并在您的应用程序中引用 React 路由器和任何其他组件/javascript 文件。
import { Router, Switch, Route } from 'react-router-dom';
import history from './history';
const App = props => (
<Router history={history}>
<MainLayout history={history} {...props}>
<Switch>
<Route exact name="index" path="/" component={Index}/>
<Route component={NotFound}/>
</Switch>
</MainLayout>
</Router>
);
(history.js)
import createBrowserHistory from 'history/createBrowserHistory';
export default createBrowserHistory();
对于 react-router-dom v4
为了获得 Router 组件的属性,我使用了 withRouter,我想下面的更改应该有效,
export default wihtRouter(MainLayout);
这应该可以在 MainLayout
中使用 props.history