admin管理员组

文章数量:1333389

I've created a react project with the instructions from it's documentation, which automatically has added the App.js file to the project. My issue is that I want to use App.tsx instead of App.js. For some reason, I am not able to run App.tsx file. How can I fix this, please?

I have read this question asked here, but I couldn't use it as it didn't make any sense to me since this is my first project with react.

This is my folder structure.

It can be seen on this image, I get an error on the browser when I ment it out the App.js file.

This is the index.js file, which is not changed.

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(<App />, document.getElementById('root'));

serviceWorker.unregister();

This is the package.json file

{
  "name": "dualnback",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.4.0",
    "@testing-library/user-event": "^7.2.0",
    "@types/lodash": "^4.14.149",
    "@types/material-ui": "^0.21.7",
    "atob": "^2.1.2",
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.19.0",
    "bootstrap": "^4.4.1",
    "concurrently": "^5.0.0",
    "cookie-parser": "^1.4.4",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "express-session": "^1.17.0",
    "express-ws": "^4.0.0",
    "hammerjs": "^2.0.8",
    "jsonwebtoken": "^8.5.1",
    "lodash": "^4.17.15",
    "material-ui": "^0.20.2",
    "mongoose": "^5.7.8",
    "morgan": "^1.9.1",
    "passport": "^0.4.0",
    "passport-jwt": "^4.0.0",
    "passport-local": "^1.0.0",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.3.0",
    "reactstrap": "^8.2.0",
    "rxjs": "~6.4.0",
    "rxjs-pat": "^6.5.3",
    "tslib": "^1.10.0",
    "typescript": "^3.7.3",
    "websocket": "^1.0.30",
    "zone.js": "~0.9.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "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"
    ]
  },
  "devDependencies": {
    "@types/react-router-dom": "^5.1.3",
    "@types/reactstrap": "^8.2.0",
    "react-router-dom@next":"*"
  }
}

I've created a react project with the instructions from it's documentation, which automatically has added the App.js file to the project. My issue is that I want to use App.tsx instead of App.js. For some reason, I am not able to run App.tsx file. How can I fix this, please?

I have read this question asked here, but I couldn't use it as it didn't make any sense to me since this is my first project with react.

This is my folder structure.

It can be seen on this image, I get an error on the browser when I ment it out the App.js file.

This is the index.js file, which is not changed.

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(<App />, document.getElementById('root'));

serviceWorker.unregister();

This is the package.json file

{
  "name": "dualnback",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.4.0",
    "@testing-library/user-event": "^7.2.0",
    "@types/lodash": "^4.14.149",
    "@types/material-ui": "^0.21.7",
    "atob": "^2.1.2",
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.19.0",
    "bootstrap": "^4.4.1",
    "concurrently": "^5.0.0",
    "cookie-parser": "^1.4.4",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "express-session": "^1.17.0",
    "express-ws": "^4.0.0",
    "hammerjs": "^2.0.8",
    "jsonwebtoken": "^8.5.1",
    "lodash": "^4.17.15",
    "material-ui": "^0.20.2",
    "mongoose": "^5.7.8",
    "morgan": "^1.9.1",
    "passport": "^0.4.0",
    "passport-jwt": "^4.0.0",
    "passport-local": "^1.0.0",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.3.0",
    "reactstrap": "^8.2.0",
    "rxjs": "~6.4.0",
    "rxjs-pat": "^6.5.3",
    "tslib": "^1.10.0",
    "typescript": "^3.7.3",
    "websocket": "^1.0.30",
    "zone.js": "~0.9.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "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"
    ]
  },
  "devDependencies": {
    "@types/react-router-dom": "^5.1.3",
    "@types/reactstrap": "^8.2.0",
    "react-router-dom@next":"*"
  }
}
Share Improve this question edited Mar 20, 2020 at 14:28 keikai 15.2k10 gold badges55 silver badges72 bronze badges asked Dec 17, 2019 at 20:49 user10728848user10728848 1331 gold badge3 silver badges15 bronze badges 7
  • Did you change what index.js imports as well? Really both of them should be renamed. – zero298 Commented Dec 17, 2019 at 21:05
  • No I haven't done any changes with index.js file – user10728848 Commented Dec 17, 2019 at 21:06
  • Does it say import {} from "./App.js" or import {} from "./App.tsx" or maybe even import {} from "./App"? – zero298 Commented Dec 17, 2019 at 21:06
  • I added the index.js code. – user10728848 Commented Dec 17, 2019 at 21:09
  • Any package.json code? – keikai Commented Dec 17, 2019 at 21:12
 |  Show 2 more ments

1 Answer 1

Reset to default 4

You may just need to install related type libs.

npm install --save @types/react @types/react-dom

Try it out.

本文标签: javascriptHow do I use Apptsx instead of Appjs in ReactStack Overflow