1

I am trying to use these variables in my scss file so that I can change my theme in the future directly from here. But it shows me parsing error, which was not there in the previous projects.

This is a React CRA project with typescript.

//App.tsx

import React from "react";
import "./App.scss";

const lightTheme = {
    "--primary": "#27AE60",
    "--text-primary": "#000000",
    "--background-primary": "#ffffff",
    "--background-secindary": "#fafafa",
} as React.CSSProperties;

const App = () => {
    return <div className="App" style={lightTheme}></div>;
};

export default App;

I can now use them as -

//App.scss

body {
color: var(--primary);
}

My package.json file -

{
    "name": "management-portal",
    "version": "0.1.0",
    "private": true,
    "dependencies": {
        "@testing-library/jest-dom": "^5.11.4",
        "@testing-library/react": "^11.1.0",
        "@testing-library/user-event": "^12.1.10",
        "@types/jest": "^26.0.15",
        "@types/node": "^12.0.0",
        "@types/react": "^16.9.53",
        "@types/react-dom": "^16.9.8",
        "react": "^17.0.1",
        "react-dom": "^17.0.1",
        "react-router-dom": "^5.2.0",
        "react-scripts": "4.0.1",
        "sass": "^1.32.5",
        "typescript": "^4.0.3",
        "web-vitals": "^0.2.4"
    },
    "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject"
    },
    "browserslist": {
        "production": [
            ">0.2%",
            "not dead",
            "not op_mini all"
        ],
        "development": [
            "last 1 chrome version",
            "last 1 firefox version",
            "last 1 safari version"
        ]
    }
}

But it is throwing me this error ...

  Line 9:3:  Parsing error: Unexpected token, expected ";"    

   7 |  "--background-primary": "#ffffff",
   8 |  "--background-secindary": "#fafafa",
>  9 | } as React.CSSProperties;
     |   ^
  10 | 
  11 | const App = () => {
  12 |  return <div className="App" style={lightTheme}></div>;

Although I expect the following result - Expected result in browser

I have got a workaround for the error, which works fine. But I am not able to find my mistake in my earlier code. Here it is -

import React from "react";
import "./App.scss";

const lightTheme: any = {
    "--primary": "#27AE60",
    "--text-primary": "#000000",
    "--background-primary": "#ffffff",
    "--background-secindary": "#fafafa",
};

const App = () => {
    return <div className="App" style={lightTheme}></div>;
};

export default App;
7
  • The linter complains about the as cast, hence I would guess that you have to tell it that you're using TypeScript (like in this bug report) Commented Jan 26, 2021 at 12:28
  • Can you add some more code from your application, because I reproduced this code in sandbox: codesandbox.io/s/divine-breeze-ci1fo?file=/src/App.tsx When i tries to do this in js file, that error showed. So see if you are actually using this in tsx file Commented Jan 26, 2021 at 12:30
  • Yes, I am using tsx only. And this error shows up, for more info, I am also attaching my package.json file - Commented Jan 26, 2021 at 12:37
  • @AakashSharma can you please make a reproducible live example (codesanbox or any you prefer Commented Jan 26, 2021 at 12:50
  • 1
    https://github.com/9267aakashsharma/management-portal You can clone it from here @MarioPetrovic Commented Jan 26, 2021 at 13:08

1 Answer 1

2

I successfully cloned your repo and found out what was the cause of the above error.

You are missing eslintConfig property in package.json:

{
  "name": "management-portal",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "@types/jest": "^26.0.15",
    "@types/node": "^12.0.0",
    "@types/react": "^16.9.53",
    "@types/react-dom": "^16.9.8",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.1",
    "sass": "^1.32.5",
    "typescript": "^4.0.3",
    "web-vitals": "^0.2.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": { <----- This one
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

Potentially you will need to add this yarn add -D eslint-config-react-app

It's a small fix, but this property is generated by CRA cli. Not sure why you are missing it since you used that command (Maybe your app was ejected or you are adding Typescript to your app ad hoc).

Sign up to request clarification or add additional context in comments.

7 Comments

It is still not working for me, I have updated the package.json but it is showing the same error. Check here - Erorr
Have you restarted your server after package.json change? It is mandatory
yes, I have restarted my server. I know that.
It's really strange. I could reproduce your error and solve it with this solution. I will check if i have anything else installed that solves this form local npm
@AakashSharma try installing this package eslint-config-react-app yarn add -D eslint-config-react-app
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.