Home > Net >  how to fix try catch in reactjs
how to fix try catch in reactjs

Time:06-28

const Login =()=> {

    const navigate = useNavigate();

    const [Data, setData] = useState({
        email:"",
        password:""
    });

    const {email,password}= Data;

    const onChangedata = (e)=> {
        
        setData({...Data,[e.target.name]:e.target.value});
    }

    const submitdata = async (e)=> {
        e.preventDefault();
       try {
  console.log(window.device.version)
} catch (e : TypeError) {
  console.log('Error')
}
    }

According to the JS reference, this is what a try-catch statement must look like I tried the followingWhich results in the error

CodePudding user response:

e: TypeError is type annotation and works only in Typescript files. If you are using JavaScript just write catch(e).

CodePudding user response:

What is the error you get?

It might be due to using the TypeError type in the catch, you can’t know what error you will get therefore you need to put unknown instead

  • Related