Home > Enterprise >  Ensure react is imported in each create-react-app component for bundling
Ensure react is imported in each create-react-app component for bundling

Time:07-27

I am using a create-react-app TypeScript project as a base for a React component I will release as an independent package. To use the package externally, I must import React from 'react' in each component file. However, create-react-app uses a babel-transform that marks the React import as unnecessary. I want my environment to expect a React import where the modular code will demand it. How can I modify my create-react-app project to throw an error if React is not imported in every component?

CodePudding user response:

You can use eslint to check it

Examples: https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/react-in-jsx-scope.md

And use it by default in VScode with extension https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint

  • Related