0

Just wonder.

If I don't want to add TS to my React project, but I need sometimes some types checking.

While for props I have a simple solution of prop-types, for state I can't do nothing.

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.

1 Answer 1

1

You can use flow instead of typescript, and for typechecking you can do it in this way :

const [name2, setName2] = useState(10);
(name2: number);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.