我从eslint处得到一个错误,说明道具验证中缺少{children},但我不知道如何为匿名函数处理这个问题。
我从eslint收到的错误消息:
src/Context/AuthContext.js
Line 7:19: 'children' is missing in props validation react/prop-typesexport 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
};发布于 2020-11-17 06:30:29
首先,您有一个错误,应该是PropTypes。
而且,children是一个反应节点,所以合适的类型是PropTypes.node。
// Not Proptypes
PropTypes.instanceOf(Object).isRequired
// Should be
AuthContext.propTypes = {
children: PropTypes.node
};https://stackoverflow.com/questions/64869972
复制相似问题