LoginSignup
3
0

More than 5 years have passed since last update.

[React Router] Property 'history' is missing in type

Posted at

エラー

Property 'history' is missing in type ...

対処法

シンプルなエラーです。

// 誤
<Route path="/" exact render={() => <TopPage hoge={hoge} />} />
// 正
<Route path="/" exact render={(props) => <TopPage {...props} hoge={hoge} />} />

解説

<Route path="/" exact component={TopPage} />

この書き方の時は、ReactRouterは、React.createElement時に route props (match, location, history) を渡してくれるが、

<Route path="/" exact render={(routeProps) => <TopPage {...routeProps} />} />

// {...hoge} は JSX Spread Attributes で、下と同じ意味
<Route path="/" exact render={(routeProps) => <TopPage history={routeProps.history} location={routeProps.location} history={routeProps.history}  />} />

この書き方の時は、route props(match, location, history) を自分で渡さないといけない。

参考

Instead of having a new React element created for you using the component prop, you can pass in a function to be called when the location matches. The render prop receives all the same route props as the component render prop.

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0