Proptypes for custom react hooks

后端 未结 4 822
长情又很酷
长情又很酷 2021-01-04 00:36

With react hooks coming, should we use prop-types for React custom hooks e.g,

import React from \'react\';
import PropTypes from \'prop-types\';    

const u         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-04 00:39

    No. React doesn't validate custom hooks or in-built hooks props.

    Look here for updateFunctionComponent, It validates prop types using checkPropTypes for a react component and then render it with hooks i.e. check out renderWithHooks.

    Now if you check here in renderWithHooks method, It updates the dispatcher and calls your functional component which in turn calls your custom hook, since it's just another function call inside your functional component.

    Your custom hook will call in-built hooks. You can check the implementation here . Based on type of dispatcher it will call built-in hooks. If you search checkPropTypes in the whole file you won't find the validation logic or prop-types/checkPropTypes dependency which is used to validate the prop types.

    Here is some nice article about how react hooks works

提交回复
热议问题