[英]How to type state in React Hooks without adding typescript (like prop-type - in a simple way)
Just wonder.只是个小疑问。
If I don't want to add TS to my React project, but I need sometimes some types checking.如果我不想将 TS 添加到我的 React 项目中,但有时我需要一些类型检查。
While for props I have a simple solution of prop-types, for state I can't do nothing.虽然对于道具我有一个简单的道具类型解决方案,但对于 state 我什么也做不了。
So is there is some solution for that?那么有什么解决办法吗?
import PropTypes from 'prop-types';
//https://reactjs.org/docs/typechecking-with-proptypes.html - prop-types
export function GreetingHook (props) {
const [name2, setName2] = useState(10);//won't generate a warning.
{
return (
<h1>Hello, {props.name}</h1>
);
}
}
GreetingHook.propTypes = {
name: PropTypes.string,
name2: PropTypes.string
};
//Some parent...
//Will generate a warning
<GreetingHook name = {5}/>
NB - The same question should be asked about class component too.注意 - 关于 class 组件也应该问同样的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.