Home > Mobile >  I want to use ?. in a React project but it throws an error
I want to use ?. in a React project but it throws an error

Time:10-04

I'm facing a problem with react, if I use ?. it throws an error

Module parse failed: Unexpected token (21:22)
You may need an appropriate loader to handle this file type.
|   var navigate = useNavigate();
|   var location = useLocation();
>   var from = location?.state?.from?.pathname || "/";      
|   var userRef = React.useRef();
|   var errRef = React.useRef();

How can I solve this?

CodePudding user response:

You'll need to either upgrade to webpack 5 or use @babel/plugin-proposal-optional-chaining

Reference: Webpack issues

CodePudding user response:

The Optional Chaining ?. operator wasn't added to Node until version 14. Make sure you have the right version installed. You can check which version of node you are using with node -v

You could also use a transcompiler like Babel which will implement unsupported features to make your code 'backwards compatible'

See: https://babeljs.io/docs/en/babel-plugin-syntax-optional-chaining.html

  • Related