![](/img/trans.png)
[英]Warning: Failed prop type: Invalid prop 'component' supplied to 'Route' - react-router-dom
[英]react-router-dom Failed prop type: Invalid prop `exact` of type `string`
当我尝试使用<Route />
组件时遇到了一些奇怪的警告。 请注意<Route exact={"true"}.../>
这行代码示例下方描述的奇怪浏览器警告。
ReactDOM.render(
<Provider store={appStore}>
<ConnectedRouter store={appStore} history={history}>
<BrowserRouter>
<Switch>
<Route exact={"true"} path="/" component={App}/>
<Route render={() => <h1>404, not found</h1>} />
</Switch>
</BrowserRouter>
</ConnectedRouter>
</Provider>,
document.getElementById('root'));
浏览器控制台接下来说我:
警告:道具类型失败:提供给
exact
的string
类型的道具Route
,应为boolean
。 在路线中(在 src/index.tsx:19)index.js:1452
上一个警告之后的以下警告完全符合文本逻辑
警告:对于非布尔属性
exact
接收到true
。如果您想将其写入 DOM,请改为传递一个字符串:exact="true" 或 exact={value.toString()}。 在一个(由 Context.Consumer 创建)中 在链接(在 App.tsx:25)中 在标头(在 App.tsx:11)中 在 div(在 App.tsx:10)中 在 App(由 Context.Consumer 创建)中 在路由中(at src/index.tsx:19) 在 Switch (at src/index.tsx:18) 在 Router (at src/index.tsx:18) 在 BrowserRouter (at src/index.tsx:17) 在 Router (at src/index.tsx:18) 在 ConnectedRouter (在 src/index.tsx:16)在提供商中(在 src/index.tsx:15)
描述的示例位于开源 prj https://github.com/gyerts/react/blob/master/starters/typescript-scss-redux/src/index.tsx#L19
问题是出于某种原因,我将属性exact
地传递给了链接组件。
<Link exact to="/about">About the author</Link>
所以我删除了exact
属性并且警告消失了。
<Link to="/about">About the author</Link>
您可以使用精确的。 您不必将其删除。
像这样:
ReactDOM.render(
<Provider store={appStore}>
<ConnectedRouter store={appStore} history={history}>
<BrowserRouter>
<Switch>
<Route exact={true} path="/" component={App}/>
<Route render={() => <h1>404, not found</h1>} />
</Switch>
</BrowserRouter>
</ConnectedRouter>
</Provider>,
document.getElementById('root'));
你可以通过true
给它。 它可以解决你的问题。 您不必删除exact
。
您可以只使用exact
属性而不使用它的值,如下所示:
<Route exact path="/" component={App}/>
你可以试试, exact={true} 。 它对我有用。
您还可以给它一个boolean
值,如下所述:
<Route exact={true} path="/" component={App}/>
或者没有任何值,当你只写属性时,被认为是真的(默认值是真的)
<Route exact path="/" component={App}/>
它对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.