Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

emotion-js on Create React App - 'jsx' is defined but never used

So I tried to use emotion-js for the first time and get hooked by the css prop feature.

While trying what the documentation says I get a warning from the eslint.

'jsx' is defined but never used @typescript-eslint/no-unused-vars

The script that I use looks like this.

import React from "react";
//** @jsx jsx */
import { jsx } from "@emotion/core";

export const Component = () => {
    return (
        <div css={{color: red}}>
            This is a component
        </div>
    )
}

I'm using VSCode, so I can see that this import is tagged as never used. (Has transparent color)

The jsx import is transparent (Never used)

But I did use it for my div, and if I remove the import, my css prop is showing an error.

Please help as to how to avoid this eslint warning, or at least make the VSCode recognize that jsx is being used.

Thanks!


Edit: (Adding reference)

Reference: https://emotion.sh/docs/css-prop#jsx-pragma


Edit 2: I tried adding .eslintrc file that looked like this

{
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint"],
  "extends": ["plugin:@typescript-eslint/recommended"],
  "rules": {
    "@typescript-eslint/no-unused-vars": [
      2,
      { "vars": "all", "args": "all", "varsIgnorePattern": "^jsx$" }
    ]
  }
}

Still get the warning, did I do it wrong?

like image 393
Michael Harley Avatar asked Feb 22 '20 18:02

Michael Harley


People also ask

Does create react app use JSX?

JSX is not a requirement for using React. Using React without JSX is especially convenient when you don't want to set up compilation in your build environment. Each JSX element is just syntactic sugar for calling React. createElement(component, props, ...children) .


1 Answers

Emotion provides its own eslint plugin that should handle this automatically for you.

Another approach is to use emotion's babel plugin to automatically add the jsx import and jsx pragma comment automatically at compile time. This way, you no longer need to add them in each of your file.

like image 70
Jackyef Avatar answered Sep 18 '22 12:09

Jackyef