我如何为匿名函数设置道具验证
how to i set props validation for an anonymous function
我从 eslint 收到一个错误,指出 {children} 在 props 验证中丢失,但我不确定如何为匿名函数处理这个问题
我从 eslint 收到的错误消息:
src/Context/AuthContext.js
Line 7:19: 'children' is missing in props validation react/prop-types
export default ({ children }) => {
const [user, setUser] = useState(null);
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [isLoaded, setIsLoaded] = useState(false);
useEffect(() => {
AuthService.isAuthenticated().then((data) => {
setUser(data.user);
setIsAuthenticated(data.isAuthenticated);
setIsLoaded(true);
});
}, []);
return (
<div>
{!isLoaded
? <h1>Loading</h1>
: (
<AuthContext.Provider value={{
user, setUser, isAuthenticated, setIsAuthenticated,
}}
>
{children}
</AuthContext.Provider>
)}
</div>
);
};
AuthContext.propTypes = {
children: Proptypes.instanceOf(Object).isRequired, //this doesnt work
};
首先,你打错了,应该是PropTypes
.
此外,children
是一个React节点,所以正确的类型是PropTypes.node
。
// Not Proptypes
PropTypes.instanceOf(Object).isRequired
// Should be
AuthContext.propTypes = {
children: PropTypes.node
};
我从 eslint 收到一个错误,指出 {children} 在 props 验证中丢失,但我不确定如何为匿名函数处理这个问题
我从 eslint 收到的错误消息:
src/Context/AuthContext.js
Line 7:19: 'children' is missing in props validation react/prop-types
export default ({ children }) => {
const [user, setUser] = useState(null);
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [isLoaded, setIsLoaded] = useState(false);
useEffect(() => {
AuthService.isAuthenticated().then((data) => {
setUser(data.user);
setIsAuthenticated(data.isAuthenticated);
setIsLoaded(true);
});
}, []);
return (
<div>
{!isLoaded
? <h1>Loading</h1>
: (
<AuthContext.Provider value={{
user, setUser, isAuthenticated, setIsAuthenticated,
}}
>
{children}
</AuthContext.Provider>
)}
</div>
);
};
AuthContext.propTypes = {
children: Proptypes.instanceOf(Object).isRequired, //this doesnt work
};
首先,你打错了,应该是PropTypes
.
此外,children
是一个React节点,所以正确的类型是PropTypes.node
。
// Not Proptypes
PropTypes.instanceOf(Object).isRequired
// Should be
AuthContext.propTypes = {
children: PropTypes.node
};