I'm creating a reusable component for a project that I have. This component has a user and a setUser as props like the example bellow.
const UserComponent = ({ user, setUser }) => {
const calcHandler = useCallback(() => {
setUser(prevUser => {
// ...some calculation
return userCopy;
});
}, [setUser]);
return (
<div>
... some content
</div>
);
};
As you can see this component has a method to calculate some operations called calcHandler, and there is only one dependency.
This is the proptype validation:
UserComponent.propTypes = {
user: PropTypes.shape({
// some props
}).isRequired,
setUser: PropTypes.func.isRequired,
};
However, for the prop setUser it is mandatory to be like a setState method (one from the useState hook). Because I can get the previeous value from it and prevent to add other dependency to the useCallback.
setUser(prevUser => //... some calculation);
I need to improve the validation of the prop setUser. It isn't just a function. Does anyone know how to do it?