Linting a React app with Nx

Share this video with your friends

Send Tweet
Published a year ago
Updated 10 months ago

Nx already comes with a default TypeScript setup. Although you can opt-out of that and generate plain JS based components, especially in a large setup TypeScript can be beneficial in uncovering potential issues early. To add an extra level, Nx also comes with preconfigured eslint for all the apps and libraries. In this lesson we're going to explore the lint setup and how it can be used.

Instructor: [0:00] Besides of working web server setup, a build setup, test setup, Nx also comes with some ESLint rule already predefined and set up for you.

[0:09] At the very root of the workspace, you see that eslintrc file, which contains the variety of predefined rules which are being considered best practice especially in a monorepo situation or monorepo workspace.

[0:21] If you open up the workspace.json again, and we go to our store project or any project you would like, you will see a lint target set up for us. It has a builder here which comes from a Novell linter package, which will run the linting process and a variety of options here that are being passed in.

[0:38] Again, in order to run the lint command, you can use the nx run command, run store:lint, and it will now lint our project for potential issues.

[0:47] In fact, you can see here in our App.jsx file, we have a problem that the React took useEffect as a missing dependency state. Let's go and see that. If we scroll in, you can also see that the Visual Studio Code correctly detects that rule violation, as we also get that hover state here. The problem is that we use the state variable here, which we don't reference as a dependency.

[1:10] There is a better way to do that. We need to just wrap that state in a function callback. We do something like that, we get the state as a primary in there, we use that, and we manipulate it here. Similarly, we also do it here and also here.

[1:29] If we run the linting rule another time, we should be good and have no errors at all. Awesome, so all files pass our linting. Also, this works for all of the variety of projects which we have in our workspace in the very same way.