user19431501
user19431501

Reputation:

how to fix try catch in reactjs

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

Upvotes: 0

Views: 59

Answers (2)

Daniel Sinai
Daniel Sinai

Reputation: 135

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

Upvotes: 0

Otarius
Otarius

Reputation: 56

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

Upvotes: 1

Related Questions