最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot pass down history prop to Component - Stack Overflow

programmeradmin1浏览0评论

I want to pass down the history prop down from the App to the Navigation ponent. When I try to do so, I get the following error message:

Failed prop type: The prop history is marked as required in Navigation, but its value is undefined.

How can I resolve this issue?

App.js:

import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';

const App = props => (
  <Router>
      <MainLayout {...props}>
        <Switch>
          <Route exact name="index" path="/" ponent={Index}/>
          <Route ponent={NotFound}/>
        </Switch>
      </MainLayout>
  </Router>
);

MainLayout.js:

import React from "react";
import PropTypes from "prop-types";
import Navigation from "../../ponents/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;

I want to pass down the history prop down from the App to the Navigation ponent. When I try to do so, I get the following error message:

Failed prop type: The prop history is marked as required in Navigation, but its value is undefined.

How can I resolve this issue?

App.js:

import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';

const App = props => (
  <Router>
      <MainLayout {...props}>
        <Switch>
          <Route exact name="index" path="/" ponent={Index}/>
          <Route ponent={NotFound}/>
        </Switch>
      </MainLayout>
  </Router>
);

MainLayout.js:

import React from "react";
import PropTypes from "prop-types";
import Navigation from "../../ponents/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;
Share Improve this question edited Apr 17, 2018 at 20:46 Tom asked Oct 26, 2017 at 20:11 TomTom 4,6825 gold badges26 silver badges53 bronze badges 2
  • Does history have a value inside of MainLayout? Your error suggests that it is undefined by the time Navigation is getting it. It also seems that App is receiving the history prop. Try tracking that prop and ensure that it is defined before it gets passed down. – Parker Ziegler Commented Oct 26, 2017 at 20:19
  • The value of history inside MainLayout is undefined , but inside App it is assigned a value – Tom Commented Oct 26, 2017 at 20:26
Add a ment  | 

2 Answers 2

Reset to default 5

SOLUTION #1:

If you simply convert <MainLayout /> to a <Route /> that renders you will have access to the history object.

<Route render={(props) => 
  <MainLayout {...props}>
    <Switch>
      <Route exact name="index" path="/" ponent={Index}/>
      <Route ponent={NotFound}/>
    </Switch>
  </MainLayout>
}/>

https://github./ReactTraining/react-router/blob/master/packages/react-router/modules/Route.js

<App /> does not have access to history as a prop so this will never do what you are wanting <MainLayout {...props}>

SOLUTION #2

You can also reference the history object as a single exported module in your app and refer to that both React router and any other popent / javascript file in your app.

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="/" ponent={Index}/>
          <Route ponent={NotFound}/>
        </Switch>
      </MainLayout>
  </Router>
);

(history.js)

import createBrowserHistory from 'history/createBrowserHistory';

export default createBrowserHistory();

https://www.npmjs./package/history

For react-router-dom v4

In order to get Router ponent's prop I used the withRouter, I guess the below change should work,

export default wihtRouter(MainLayout);

This should enable the usage of props.history in MainLayout

发布评论

评论列表(0)

  1. 暂无评论