我正在使用@reach/router
React。我创建了一个带有所需道具的路由组件。当应用程序安装但路线未激活时,我会收到道具类型警告。
这个简单的例子改编自@reach/router
docs。
import React from "react";
import { render } from 'react-dom';
import PropTypes from 'prop-types';
import { Router, Link } from "@reach/router";
const App = ({ children }) => (
<div>
<nav>
<Link to="/">Home</Link>{" "}
<Link to="users/123">Bob</Link>{" "}
<Link to="users/abc">Sally</Link>
</nav>
{children}
</div>
);
const User = props => <h2>{props.userId}</h2>;
User.propTypes = {
userId: PropTypes.string.isRequired
};
const Home = () => (
<div>
<h2>Welcome!</h2>
<p>
Select a user, their ID will be parsed from the URL and passed to the User
component
</p>
</div>
);
render(
<Router>
<App path="/">
<Home path="/" />
<User path="users/:userId" />
</App>
</Router>,
document.getElementById("root")
);
代码呈现Home
组件而不是User
组件。尽管如此,我可以在控制台中看到以下错误:
Failed prop type: The prop `userId` is marked as required in `User`, but its value is `undefined`.
除非渲染组件,否则我不希望看到此错误。
有没有办法防止这种情况发生?
我想使用 指定动态段中的道具是必需的prop-types
,但我不想看到这个道具警告。