How do I use App.tsx instead of App.js in React? - javascript

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 comment 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-compat": "^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":"*"
}
}

You may just need to install related type libs.
npm install --save #types/react #types/react-dom
Try it out.

Related

How to change React Base URL from localhost to my custom domain

I just created my react-app and everything is working on my localhost also connecting to backend app through API which i configured, Now I just deployed my react app to nginx server after running yarn run build and i uploaded the build folder to my server root directory. The home page was serve and other page which needn't to connect to the server was serve as well but when I try to login I got an error and from the error I realised my app was sending request to http://localhost:3000/v1/auth/login instead of http://api.mysite.com/v1/auth/login How do I make this changes before building my react app. Below is my package.json file
{
"name": "mysite",
"version": "0.1.0",
"private": true,
"proxy": "https://api.mysite.com",
"dependencies": {
"#date-io/dayjs": "^2.16.0",
"#emotion/react": "^11.10.4",
"#emotion/styled": "^11.10.4",
"#fortawesome/free-solid-svg-icons": "^6.2.0",
"#material-ui/core": "^4.12.4",
"#mui/icons-material": "^5.10.2",
"#mui/material": "^5.10.17",
"#mui/x-date-pickers-pro": "^5.0.12",
"#testing-library/jest-dom": "^5.16.5",
"#testing-library/react": "^13.3.0",
"#testing-library/user-event": "^13.5.0",
"axios": "^0.27.2",
"bootstrap": "^5.2.0",
"jquery": "^3.6.0",
"mdb-react-ui-kit": "^4.1.0",
"moment": "^2.29.4",
"popper.js": "^1.16.1",
"react": "^18.2.0",
"react-bootstrap": "^2.5.0",
"react-bootstrap-submenu": "^2.0.0",
"react-countdown": "^2.3.5",
"react-dom": "^18.2.0",
"react-fontawesome": "^1.7.1",
"react-icons": "^4.4.0",
"react-image-gallery": "^1.2.9",
"react-loading-skeleton": "^3.1.0",
"react-phone-number-input": "^3.2.9",
"react-pro-sidebar": "^1.0.0-alpha.7",
"react-redux": "^8.0.2",
"react-responsive": "^9.0.0-beta.10",
"react-responsive-carousel": "^3.2.23",
"react-router": "^6.3.0",
"react-router-dom": "^6.3.0",
"react-scripts": "^5.0.1",
"react-select": "^5.6.1",
"react-simple-star-rating": "^4.0.5",
"react-slick": "^0.29.0",
"redux": "^4.2.0",
"semantic-ui-react": "^2.1.3",
"slick-carousel": "^1.8.1",
"styled-components": "^5.3.5",
"sweetalert": "^2.1.2",
"swiper": "^8.3.2",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts --openssl-legacy-provider start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#babel/plugin-proposal-optional-chaining": "^7.18.9",
"#expo/webpack-config": "^0.17.2",
"react-error-overlay": "6.0.9"
},
"resolutions": {
"styled-components": "^5",
"//": "See https://github.com/facebook/create-react-app/issues/11773",
"react-error-overlay": "6.0.9"
}
}
Proxies only work for the dev server and don't work in production. You'll have to create an env file and use that instead. Here's how you'd do it:
// .env
REACT_APP_APP_HOST='https://api.mysite.com'
process.env.REACT_APP_APP_HOST // use this to access your api

i got a problem when i use MUI with CRA + TS

i dont know why it is issue occured
An undefined error occurs when using the every MUI tag .
import { Button } from "#mui/material";
// import Button from "#mui/material/Button";
interface Props {}
export default function Test(props: Props) {
return (
<Button>asd</Button>
);
}
return _extends({}, theme.typography.button, { <--- error
Uncaught TypeError: Cannot read properties of undefined (reading 'button')
at ButtonRoot.ownerState.ownerState (Button.js:68:1)
at transformedStyleArg (createStyled.js:191:1)
at handleInterpolation (emotion-serialize.browser.esm.js:139:1)
at serializeStyles (emotion-serialize.browser.esm.js:264:1)
at emotion-styled-base.browser.esm.js:131:1
at emotion-element-cbed451f.browser.esm.js:36:1
at renderWithHooks (react-dom.development.js:16305:1)
at updateForwardRef (react-dom.development.js:19226:1)
at beginWork (react-dom.development.js:21636:1)
at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1)
below is my package info
{
"name": "foo",
"version": "0.1.0",
"private": true,
"dependencies": {
"#craco/craco": "^6.4.3",
"#emotion/react": "^11.9.3",
"#emotion/styled": "^11.9.3",
"#hookform/error-message": "^2.0.0",
"#material-ui/core": "^4.12.4",
"#mui/icons-material": "^5.8.4",
"#mui/material": "^5.9.2",
"#mui/styles": "^5.9.2",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.3.0",
"#testing-library/user-event": "^13.5.0",
"#types/jest": "^27.5.2",
"#types/node": "^16.11.45",
"#types/react": "^18.0.15",
"#types/react-dom": "^18.0.6",
"axios": "^0.27.2",
"classnames": "^2.3.1",
"dayjs": "^1.11.3",
"framer-motion": "^6.3.12",
"lodash": "^4.17.21",
"react": "^18.2.0",
"react-calendar": "^3.7.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.33.1",
"react-material-ui-carousel": "^3.4.2",
"react-query": "^3.39.1",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"recharts": "^2.1.12",
"typescript": "^4.7.4",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "craco start",
"build": "craco build",
"test": "craco test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#emotion/babel-preset-css-prop": "^11.2.0",
"#types/lodash": "^4.14.182",
"#types/react-calendar": "^3.5.1",
"autoprefixer": "^10.4.7",
"craco-alias": "^3.0.1",
"postcss": "^8.4.14",
"tailwindcss": "^3.1.4"
}
}
i dont know reason why
In my opinion, several CSS libraries are overlapping to create an issue
so now I'm removing them one by one
but i got still problem
i'm sorry about not good at english

React project stopped working due to Webpack version

My project was working correctly 100% until today when I started my development environment and it gave me a bunch of errors. The errors indicated my adding fallbacks for modules like HTTP and crypto. I did that and it gave me the error of Module fs not found.
I researched on the internet and downgraded my react-scripts from v5 to v4.0.3 and now when I start the project it gives me the following error
./node_modules/react-toastify/dist/react-toastify.esm.mjs
Can't import the named export 'cloneElement' from non EcmaScript module (only default export is available)
Error from chokidar (C:\node_modules): Error: EBUSY: resource busy or locked, lstat 'xxx:\DumpStack.log.tmp'
My package.json
{
"name": "ahad",
"version": "0.1.0",
"private": true,
"dependencies": {
"#mui/icons-material": "^5.8.4",
"#stripe/stripe-js": "^1.32.0",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.3.0",
"#testing-library/user-event": "^13.5.0",
"#types/node": "^18.0.0",
"apexcharts": "^3.35.3",
"axios": "^0.27.2",
"browserify-zlib": "^0.2.0",
"crypto-browserify": "^3.12.0",
"datetime-difference": "^1.0.2",
"express-validator": "^6.14.2",
"formik": "^2.2.9",
"ip-info-finder": "^1.2.3",
"mdb-react-ui-kit": "^4.0.0",
"mdbreact": "^5.2.0",
"path-browserify": "^1.0.1",
"react": "^18.2.0",
"react-apexcharts": "^1.4.0",
"react-bootstrap": "^2.4.0",
"react-dom": "^18.2.0",
"react-gif-loader": "^0.6.0",
"react-icons": "^4.4.0",
"react-loading": "^2.0.3",
"react-pro-sidebar": "^0.7.1",
"react-router-dom": "^6.3.0",
"react-scripts": "4.0.3",
"react-select": "^5.3.2",
"react-stripe-checkout": "^2.6.3",
"react-toastify": "^9.0.5",
"socket.io-client": "^4.5.1",
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"stripe": "^9.9.0",
"web-vitals": "^2.1.4",
"yup": "^0.32.11"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

React Ant design Uncaught TypeError: Cannot read property 'Component' of undefined

My app runs successfully but I am getting this error on browser console after using a few components of ant design.
When I installed the ant d I done these steps
npm install antd
added link tag of antd.compact.min.css to index.html
added script tag of /antd/4.13.1/antd.min.js to index.html
added script tag of /antd/4.13.1/antd-with-locales.min.js to index.html
Uncaught TypeError: Cannot read property 'Component' of undefined
at index.js:165
at Module.a.m.i (index.js:14)
at a (bootstrap:19)
at Object.a.m.i (index.js:25)
at a (bootstrap:19)
at Object.a.m.i (antd.min.js:23)
at a (bootstrap:19)
at bootstrap:83
at universalModuleDefinition:9
at universalModuleDefinition:1
Here is my package.json
{
"name": "example",
"version": "0.1.0",
"private": true,
"dependencies": {
"#ant-design/icons": "^4.6.2",
"#material-ui/core": "^4.11.3",
"#material-ui/styles": "^4.11.3",
"#react-google-maps/api": "^2.1.1",
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"antd": "^4.13.1",
"axios": "^0.21.1",
"bootstrap": "^4.6.0",
"customize-cra": "^1.0.0",
"jsonwebtoken": "^8.5.1",
"jwt-decode": "^3.1.2",
"moment": "^2.29.1",
"react": "^17.0.1",
"react-app-rewired": "^2.1.8",
"react-dom": "^17.0.1",
"react-file-base64": "^1.0.3",
"react-google-maps": "^9.4.5",
"react-hook-google-maps": "0.0.3",
"react-icons": "^4.2.0",
"react-moment": "^1.1.1",
"react-redux": "^7.2.2",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"redux": "^4.0.5",
"redux-devtools-extension": "^2.13.9",
"redux-thunk": "^2.3.0",
"styled-components": "^5.2.1",
"uuid": "^8.3.2",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
I tried import React {Component} from "react"; and import * as React from 'react'; but not worked.
I found the solution for my problem. Firstly, if you are familiar with HTML CSS and JS structure and using UI libraries(antd ,bootstrap ,semantic UI) like I used to be, this problem could meet you to when you passed to frameworks like Reactjs.
Point is, no need to add CDN links <link> and <script> to your index.html file (if documentation does not says to add).
These steps are enough,(example for ant design UI library)
also this link Use in create-react-app says same
npm install antd
#import '~antd/dist/antd.css'; to App.css
and add import { Button } from 'antd'; to top of your component when using a ant design component such as button
Conclusion, this error occurs when add and tags to react app's index.html

Material UI Picker - date-fns issue

I am using material UI in my react application and I am having difficulty getting the material-ui-pickers example to work.
Here is the online example from Material UI.
https://codesandbox.io/s/52p74362pl
The error that i am getting is:
index.js:1446 ../node_modules/#date-io/date-fns/build/index.esm.js Module not found: `Can't resolve 'date-fns/addDays' in 'C:\Project4\CDE\IFS.CDE.Web\node_modules\#date-io\date-fns\build'`
here are my package.json files
{
"name": "IFS.CDE.Web",
"version": "0.1.0",
"private": true,
"dependencies": {
"#material-ui/core": "^3.7.1",
"#material-ui/icons": "^3.0.1",
"ajv": "^6.5.2",
"autosuggest-highlight": "^3.1.1",
"axios": "^0.18.0",
"classnames": "^2.2.5",
"connected-react-router": "6.1.0",
"date-fns": "^1.30.1",
"downshift": "^2.0.10",
"extract-svg-path": "^2.1.0",
"file-saver": "^1.3.8",
"install": "^0.12.1",
"lodash": "^4.17.10",
"material-ui-datetimepicker": "^1.0.7",
"material-ui-image": "^3.0.3",
"material-ui-pickers": "^2.1.1",
"moment": "^2.23.0",
"npm": "^6.5.0",
"parse-svg-path": "^0.1.2",
"prop-types": "^15.6.2",
"rc-progress": "^2.2.6",
"react": "^16.6.3",
"react-autosuggest": "^9.3.4",
"react-bingmaps": "^3.6.1",
"react-color": "^2.14.1",
"react-dnd": "^7.0.2",
"react-dom": "^16.6.3",
"react-dropzone": "^4.3.0",
"react-file-viewer": "^0.5.0",
"react-iframe": "^1.2.0",
"react-loadable": "^5.5.0",
"react-popper": "^0.10.1",
"react-redux": "^6.0.0",
"react-render-html": "^0.6.0",
"react-router-dom": "^4.3.1",
"react-scripts": "^2.1.2",
"react-select": "^2.0.0",
"redux": "^4.0.1",
"redux-persist": "^5.10.0",
"redux-thunk": "^2.3.0",
"rimraf": "^2.6.2",
"typeface-roboto": "^0.0.54",
"uuid": "^3.3.2",
"video-react": "^0.13.1"
},
"scripts": {
"start": "rimraf ./build && react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"set-env": "set NODE_ENV=production"
},
"devDependencies": {},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
I have installed date-fns but i still get the error.
https://www.npmjs.com/package/date-fns.
I also deleted all the node module and did a npm install to see if that would solve the issue but it did not.
Thanks for the help.
install this(see here):
npm i --save date-fns#next #date-io/date-fns
Try using date-fns 2.0.0. I had the same issue and fixed by updating the lib. Hope it works.

Categories

Resources