ReactJS – .JS vs .JSX

There is none when it comes to file extensions. Your bundler/transpiler/whatever takes care of resolving what type of file contents there is. There are however some other considerations when deciding what to put into a .js or a .jsx file type. Since JSX isn’t standard JavaScript one could argue that anything that is not “plain” JavaScript should go into … Read more

How do I avoid ‘Function components cannot be given refs’ when using react-router-dom?

NavLink from react-router is a function component that is a specialized version of Link which exposes a innerRef prop for that purpose. You could’ve also searched our docs for react-router which leads you to https://mui.com/getting-started/faq/#how-do-i-use-react-router which links to https://mui.com/components/buttons/#third-party-routing-library. The last link provides a working example and also explains how this will likely change in … Read more

command not found: jest

Jest is installed, but is likely in your ./node_modules/.bin directory. You can append that to your command ./node_modules/.bin/jest –updateSnapshot. Since you already have jest as a scripts command in your package.json you can also run it with npm test — –updateSnapshot. npm automatically adds ./node_modules/.bin to your path. update: Newer versions of yarn will resolve node module bin scripts, so you can also just run yarn jest {cmd} and it … Read more

Uncaught Invariant Violation: Rendered more hooks than during the previous render

The fix works because the first code sample (the erroring one) invokes a function inside onClick, while the second (the working one) passes a function to onClick. The difference is those all-important parentheses, which in JavaScript mean ‘invoke this code’. Think of it this way: in the first code sample, every time component is rendered, renderResults is invoked. Every time that … Read more