ReactJS error when accessing page using Server-side Rendering - reactjs

I get this error when I load my ReactJS page
Error: Invariant failed: You should not use <Switch> outside a <Router>
at invariant (/home/user/Documents/Development/hmuweb/room/node_modules/tiny-invariant/dist/tiny-invariant.cjs.js:13:11)
at Object.children (/home/user/Documents/Development/hmuweb/room/node_modules/react-router/cjs/react-router.js:685:19)
at ReactDOMServerRenderer.render (/home/user/Documents/Development/hmuweb/room/node_modules/react-dom/cjs/react-dom-server.node.development.js:3635:55)
at ReactDOMServerRenderer.read (/home/user/Documents/Development/hmuweb/room/node_modules/react-dom/cjs/react-dom-server.node.development.js:3373:29)
at Object.renderToString (/home/user/Documents/Development/hmuweb/room/node_modules/react-dom/cjs/react-dom-server.node.development.js:3988:27)
at ./server/index.js.app.get (/home/user/Documents/Development/hmuweb/room/server-build/index.js:215:71)
at Layer.handle [as handle_request] (/home/user/Documents/Development/hmuweb/room/node_modules/express/lib/router/layer.js:95:5)
at next (/home/user/Documents/Development/hmuweb/room/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/user/Documents/Development/hmuweb/room/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/user/Documents/Development/hmuweb/room/node_modules/express/lib/router/layer.js:95:5)
I am assuming this error message is coming from my App.js file but I cannot seem to find where the error is occurring exactly. I tried re-structuring my App.js but when I do this, I receive another error saying the prop history is marked as required in the Router but its value is undefined. Also, that it cannot read property 'location' of undefined. Any idea how I can fix this issue? It started when I implemented SSR (Server Side Rendering).
import React, { Component } from 'react';
import { Route, Switch, BrowserRouter as Router } from 'react-router-dom';
import './App.css';
import Room from './App/pages/Room'
import Content from './App/pages/content';
class App extends Component {
render() {
return (
<div>
<Switch>
<Route exact={true} path="/" component={Room} />
<Route path="/watch" component={Content} />
</Switch>
</div>
);
}
}
export default App;
webpack.server.js file incase needed
const path = require('path');
const nodeExternals = require('webpack-node-externals');
module.exports = {
devtool: 'source-map',
entry: './server/index.js',
target: 'node',
externals: [nodeExternals()],
module: {
rules: [
{
test: /\.js$/,
use: ["babel-loader"],
exclude: /node_modules/,
},
{ test: /\.css$/, loader: "css-loader" },
{ test: /\.(jpg|png|svg)$/, use: 'file-loader'}
]
},
resolve: {
alias: {
'react-router-dom': path.join('./node_modules/react-router-dom')
}
},
output: {
path: path.resolve('server-build'),
filename: 'index.js'
},
};
package.json file incase needed
{
"name": "room",
"version": "0.1.0",
"private": true,
"dependencies": {
"#babel/plugin-proposal-class-properties": "^7.8.3",
"axios": "^0.19.2",
"babel-preset-es2015": "^6.24.1",
"bootstrap": "^3.4.1",
"branca": "^0.3.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"hls.js": "^0.13.2",
"html-react-parser": "^0.10.3",
"jquery": "^3.5.1",
"jsonwebtoken": "^8.5.1",
"object-encrypt-decrypt": "^1.0.2",
"path": "^0.12.7",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-helmet": "^6.0.0",
"react-router-dom": "^5.2.0",
"react-router-redux": "^4.0.8",
"react-scripts": "3.2.0"
},
"scripts": {
"start": "node ./server/index.js | react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"dev:build-server": "NODE_ENV=development webpack --config webpack.server.js --mode=development -w",
"dev:start": "nodemon ./server-build/index.js",
"dev": "npm-run-all --parallel build dev:*"
},
"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"
]
},
"proxy": "http://localhost:5000",
"devDependencies": {
"babel-core": "^6.26.3",
"babel-preset-env": "^1.7.0",
"babel-preset-react-app": "^9.1.2",
"nodemon": "^2.0.4",
"npm-run-all": "^4.1.5",
"webpack-cli": "^3.3.11",
"webpack-node-externals": "^1.7.2"
},
"babel": {
"presets": [
"#babel/preset-env",
"#babel/preset-react"
],
"plugins": [
"#babel/plugin-proposal-class-properties"
]
}
}
Index.js
import path from 'path';
import fs from 'fs';
import express from 'express';
import React from 'react'
import ReactDOMServer from 'react-dom/server';
import { StaticRouter } from 'react-router-dom';
import App from '../src/App';
const PORT = process.env.PORT || 5000;
const app = express();
app.use(express.static(path.join(__dirname, '../build')));
app.get('/*', (req, res) => {
const context = {};
const app = ReactDOMServer.renderToString(
<StaticRouter location={req.url} context={context}>
<App />
</StaticRouter>
);
const indexFile = path.join(__dirname + '../build/index.html');
fs.readFile(indexFile, 'utf8', (err, data) => {
if (err) {
console.error('Something went wrong:', err);
return res.status(500).send('Oops, better luck next time!');
}
if (context.status === 404) {
res.status(404);
}
return res.send(
data.replace('<div id="root"></div>', `<div id="root">${app}</div>`)
);
});
});
app.listen(PORT, () => {
console.log(`😎 Server is listening on port ${PORT}`);
});

After hours of troubleshooting, I have finally fixed this issue.
Instead of passing the directly inside the renderToString function, you have to nest it inside of a StaticRouter tag.
This answer helped me out: https://github.com/jaredpalmer/razzle/issues/1157#issuecomment-553044993
Also, I had to nest my Switch tag inside a Router tag like this:
<Router>
<Switch>
<Route exact={true} path="/" component={Room} />
<Route path="/watch" component={Content} />
</Switch>
</Router>
After doing this I received another issue where my build index.html wasn't found. To fix this issue I had to tweak my project:
In ReactJS index.js file I had to switch my ReactDOM.render to ReactDOM.hydrate instead.
Created a server.js file inside my server folder where I moved my index.js code inside.
Inside my old server/index.js, I implemented my server-side support for JSX using #babel.
Here is my code:
server/index.js
import path from 'path';
import fs from 'fs';
import express from 'express';
import React from 'react'
import ReactDOMServer from 'react-dom/server';
import { StaticRouter } from 'react-router-dom';
import App from '../src/App';
const PORT = process.env.PORT || 5000;
const app = express();
app.get('^/$', (req, res) => {
const context = {};
const app = ReactDOMServer.renderToString(
<StaticRouter location={req.url} context={context}>
<App />
</StaticRouter>
);
const indexFile = path.resolve('./build/index.html');
fs.readFile(indexFile, 'utf8', (err, data) => {
if (err) {
console.error('Something went wrong:', err);
return res.status(500).send('Oops, better luck next time!');
}
if (context.status === 404) {
res.status(404);
}
return res.send(
data.replace('<div id="root"></div>', `<div id="root">${app}</div>`)
);
});
});
app.use(express.static(path.resolve(__dirname, '..', 'build')));
app.listen(PORT, () => {
console.log(`😎 Server is listening on port ${PORT}`);
});
server/index.js
require('ignore-styles')
require('#babel/register')({
ignore: [/(node_module)/],
presets: ['#babel/preset-env', '#babel/preset-react'],
plugins: ['#babel/plugin-proposal-class-properties']
})
require('./server');
package.json
{
"name": "room",
"version": "0.1.0",
"private": true,
"homepage": "https://hmutv.com/",
"dependencies": {
"#babel/plugin-proposal-class-properties": "^7.8.3",
"#babel/preset-react": "^7.9.4",
"#babel/register": "^7.9.0",
"axios": "^0.19.2",
"babel-preset-es2015": "^6.24.1",
"bootstrap": "^3.4.1",
"branca": "^0.3.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"history": "^4.10.1",
"hls.js": "^0.13.2",
"html-react-parser": "^0.10.3",
"ignore-styles": "^5.0.1",
"jquery": "^3.5.1",
"jsonwebtoken": "^8.5.1",
"next": "^9.4.0",
"object-encrypt-decrypt": "^1.0.2",
"path": "^0.12.7",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-helmet": "^6.0.0",
"react-router-dom": "^5.2.0",
"react-router-redux": "^4.0.8",
"react-scripts": "3.2.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"ssr": "node server/index.js"
},
"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"
]
},
"proxy": "http://localhost:5000",
"devDependencies": {
"babel-core": "^6.26.3",
"babel-preset-env": "^1.7.0",
"babel-preset-react-app": "^9.1.2",
"nodemon": "^2.0.4",
"npm-run-all": "^4.1.5",
"webpack-cli": "^3.3.11",
"webpack-node-externals": "^1.7.2"
}
}

Related

React App Keep Reloading until it get crashed

I am new to react js, I created a react app, and whenever I try to open the app it continuously reloads. can anyone help me in this? i tried but unable to find out any perfect solution.
Any solution?
package.json
{
"name": "sf",
"author": "KKK Team",
"version": "0.0.1",
"private": true,
"homepage": "/sfrs",
"scripts": {
"start": "env-cmd -f .env.development react-scripts start",
"build:development": "env-cmd -f .env.development react-scripts build",
"build:nonprod": "env-cmd -f .env.nonprod react-scripts build",
"build:production": "env-cmd -f .env.production react-scripts build",
"build": "react-scripts build",
"test": "jest --verbose",
"test:dev": "jest",
"test:watch": "jest --silent --watchAll",
"test:coverage": "jest --silent --coverage",
"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"
]
},
"dependencies": {
"#date-io/date-fns": "^1.3.13",
"#emotion/react": "^11.7.1",
"#emotion/styled": "^11.6.0",
"#material-table/core": "^0.2.20",
"#mui/icons-material": "^5.3.1",
"#mui/lab": "^5.0.0-alpha.66",
"#mui/material": "^5.3.1",
"#mui/styles": "^5.3.0",
"axios": "^0.25.0",
"buffer": "^6.0.3",
"clsx": "^1.1.1",
"crypto-browserify": "^3.12.0",
"env-cmd": "^10.1.0",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
"moment": "^2.29.1",
"msal": "^1.4.16",
"prop-types": "^15.8.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-perfect-scrollbar": "^1.5.8",
"react-redux": "^7.2.6",
"react-router": "^6.2.1",
"react-router-dom": "^6.2.1",
"recompose": "^0.30.0",
"redux": "^4.1.2",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.4.1",
"stream-browserify": "^3.0.0",
"sweetalert2": "^11.4.8",
"sweetalert2-react-content": "^4.2.0",
"util": "^0.12.4",
"validator": "^13.7.0",
"web-vitals": "^2.1.4"
},
"devDependencies": {
"#testing-library/jest-dom": "^5.16.1",
"#testing-library/react": "^12.1.2",
"#testing-library/user-event": "^13.5.0",
"eslint": "^7.11.0",
"eslint-config-airbnb": "^18.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.20.3",
"eslint-plugin-react-hooks": "^4.0.8",
"husky": "^4.2.3",
"jest-canvas-mock": "^2.3.1",
"lint-staged": "^10.0.8",
"prettier": "^1.19.1",
"react-error-overlay": "^6.0.9",
"react-scripts": "4.0.3"
},
"jest": {
"setupFiles": [
"jest-canvas-mock"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged",
"pre-push": "npm run test"
}
},
"lint-staged": {
"src/**/*.{js,jsx,json,css}": [
"prettier --write",
"eslint --fix"
]
},
"babel": {
"presets": [
"#babel/preset-react",
[
"#babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
],
"plugins": [
[
"#babel/plugin-proposal-class-properties",
{
"loose": true
}
]
]
},
"resolutions": {
"react-error-overlay": "6.0.9"
}
}
I am using MUI 5. I have tried with upgrading and degrading the node version but unable to solve the issue.
app.jsx
import 'react-perfect-scrollbar/dist/css/styles.css';
import React, { useEffect } from 'react';
import { useRoutes } from 'react-router-dom';
import _ from 'lodash';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { ThemeProvider } from '#mui/material';
import AuthProvider from './components/auth_provider';
import GlobalStyles from './components/globalstyles';
import theme from './theme/theme';
import AuthenticationProgressLoader from './components/authentication_progress_loader';
import UnAuthorized from './components/unauthorized';
import routeManager from './lib/route.manager';
import Alerts from './components/c_alert';
import PageProgress from './components/c_page_progress';
import PermissionManager from './lib/permission.manager';
const App = props => {
const {
userAccount,
userPermissions,
graphProfile,
routesConfig,
alerts,
isPageLoader
} = props;
const routing = useRoutes(routesConfig || []);
useEffect(() => {
if (
!_.isEmpty(userPermissions) &&
PermissionManager.isUserMerchant() &&
_.isEmpty(routesConfig)
) {
routeManager.dispatchRoutes();
}
}, [routesConfig, userPermissions]);
return !userPermissions ||
(!_.isEmpty(userPermissions) && !PermissionManager.isUserMerchant()) ? (
<UnAuthorized />
) : (
<ThemeProvider theme={theme}>
<GlobalStyles />
{!_.isEmpty(alerts.message) && <Alerts />}
{isPageLoader && <PageProgress />}
{userAccount && graphProfile && !_.isEmpty(userPermissions) ? (
routing
) : (
<AuthenticationProgressLoader />
)}
</ThemeProvider>
);
};
App.propTypes = {
// eslint-disable-next-line react/forbid-prop-types
userAccount: PropTypes.object,
// eslint-disable-next-line react/forbid-prop-types
graphProfile: PropTypes.object,
// eslint-disable-next-line react/forbid-prop-types
userPermissions: PropTypes.object,
// eslint-disable-next-line react/forbid-prop-types
routesConfig: PropTypes.array,
// eslint-disable-next-line react/forbid-prop-types
alerts: PropTypes.object,
isPageLoader: PropTypes.bool
};
App.defaultProps = {
userAccount: {},
graphProfile: {},
userPermissions: {},
routesConfig: [],
alerts: {},
isPageLoader: false
};
function mapStatesToProps(state) {
const { userPermissions, graphProfile, userAccount } = state.auth;
const { routesConfig } = state.routes;
const { alerts } = state;
const { isPageLoader } = state.pageProgress;
return {
userPermissions,
graphProfile,
userAccount,
routesConfig,
alerts,
isPageLoader
};
}
const authApp = AuthProvider(App);
const connectedApp = connect(mapStatesToProps, null)(authApp);
// eslint-disable-next-line
export { connectedApp as App };

Module parse failed: Unexpected token React/Typescript

i tried to setup a new Project with webpack, react and Typescript. Its not working.
Webpack is giving me this error:
ERROR in ./src/index.tsx 17:2
Module parse failed: Unexpected token (17:2)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.
See https://webpack.js.org/concepts#loaders
ERROR
|
| ReactDOM.render(
> <StoreContext.Provider value={store}>
| <Router history={history}>
| <App />
# ./src/index.tsx 2:0-36 6:17-24 7:15-29
webpack.config.js
const { resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const TerserWebpackPlugin = require('terser-webpack-plugin');
const isProd = process.env.NODE_ENV === 'production';
const config = {
mode: isProd ? 'production' : 'development',
entry: {
index: './src/index.tsx',
},
plugins: [
new HtmlWebpackPlugin({
title: 'Production',
}),
],
output: {
path: resolve(__dirname, 'dist'),
filename: '[name].js',
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
},
module: {
rules: [
{
test: /\.tsx?$/,
use: ['babel-loader', 'style-loader'],
exclude: /node_modules/,
},
],
},
};
if (isProd) {
config.optimization = {
minimizer: [
new TerserWebpackPlugin(),
],
};
} else {
config.devServer = {
port: 8080,
open: true,
hot: true,
compress: true,
stats: 'errors-only',
overlay: true,
};
}
module.exports = config;
package.json:
{
"scripts": {
"watch": "webpack --watch",
"start": "webpack serve --open",
"build": "webpack"
},
"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/cli": "^7.14.5",
"#babel/core": "^7.14.6",
"#babel/preset-env": "^7.14.7",
"#babel/preset-react": "^7.14.5",
"#babel/preset-typescript": "^7.14.5",
"#types/node": "^12.20.15",
"#types/react-datepicker": "^3.1.8",
"#types/react-dom": "^17.0.8",
"#types/uuid": "^8.3.0",
"#types/yup": "^0.29.11",
"#typescript-eslint/eslint-plugin": "^4.28.0",
"#typescript-eslint/parser": "^4.28.0",
"babel-loader": "^8.2.2",
"cross-env": "^7.0.3",
"css-loader": "^5.2.6",
"eslint": "^7.29.0",
"eslint-plugin-react": "^7.24.0",
"eslint-plugin-react-hooks": "^4.2.0",
"html-webpack-plugin": "^5.3.2",
"serve": "^12.0.0",
"style-loader": "^2.0.0",
"terser-webpack-plugin": "^5.1.3",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"typescript": "^4.3.4",
"typings-for-css-modules-loader": "^1.7.0",
"webpack": "^5.40.0",
"webpack-cli": "^4.7.2",
"webpack-dev-server": "^3.11.2",
"webpack-merge": "^5.8.0",
"yup": "^0.32.9"
}
}
Index.tsx:
import ReactDOM from 'react-dom';
import './index.css';
import './app/layout/styles.css'
import App from './app/layout/App';
import reportWebVitals from './reportWebVitals';
import 'semantic-ui-css/semantic.min.css'
import {store, StoreContext } from './app/stores/store';
import {createBrowserHistory} from 'history';
import { Router} from "react-router-dom";
import 'react-toastify/dist/ReactToastify.min.css';
import 'react-datepicker/dist/react-datepicker.css';
export const history = createBrowserHistory();
ReactDOM.render(
<StoreContext.Provider value={store}>
<Router history={history}>
<App />
</Router>
</StoreContext.Provider>,
document.getElementById('root')
);
reportWebVitals();

Module not found: Can't resolve 'apollo-link-context' - REACT, APOLLO-SERVER

I have a backend with users that i want to display on my frontend folder.
In order to do that i need authorization headers to be implemented in React via Apollo Server.
I followed some instruction from here -> https://www.apollographql.com/docs/react/networking/authentication/
This is my code inside App.js
import React from 'react';
import HeroesDota from './components/HeroesDota';
import { ApolloProvider } from '#apollo/react-hooks';
import { ApolloClient } from "apollo-client";
import { InMemoryCache } from "apollo-cache-inmemory";
import { HttpLink } from "apollo-link-http";
import { setContext } from 'apollo-link-context';
const httpLink = new HttpLink({
uri: "http://localhost:8000/graphql/",
});
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache()
});
const authLink = setContext((_, { headers }) => {
// get the authentication token from local storage if it exists
const token = localStorage.getItem('token');
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : "",
}
}
});
const App = () => {
return (
<ApolloProvider client={client}>
<HeroesDota />
</ApolloProvider>
)};
export default App;
After i run server on localhost:3000 it says: Uncaught Error: Cannot find module 'apollo-link-context'
I installed apollo-link as dependency. I dont know why this error show up. Here is the actual picture.
package.json
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"#apollo/react-hooks": "^3.1.3",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.4.0",
"#testing-library/user-event": "^7.2.1",
"apollo-boost": "^0.4.7",
"apollo-cache-inmemory": "^1.6.5",
"apollo-link": "^1.2.13",
"apollo-link-ws": "^1.0.19",
"bootstrap": "^4.4.1",
"graphql": "^14.5.8",
"react": "^16.12.0",
"react-apollo": "^3.1.3",
"react-bootstrap": "^1.0.0-beta.16",
"react-dom": "^16.12.0",
"react-router": "^5.1.2",
"react-router-dom": "^5.1.2",
"react-scripts": "3.3.0",
"styled-components": "^5.0.0",
"subscriptions-transport-ws": "^0.9.16"
},
"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": {}
}
You are importing it right here:
import { setContext } from 'apollo-link-context';
Please run npm i apollo-link-context to fix.

How can I render a react application compiled in a bundle.js?

So I have a React application that was compiled into a bundle.js file. The index.tsx file of the React application shows that the App component will be rendered in a DOM element with ID "scheduleSitesGrid2" as showed on this picture:
I have been able to bundle this react app and host the bundle on Azure Storage. In fact, if I create a simple HTML file that only contains a div and a script with a src pointing to the azure url, I am able to see the bundle.js in Chrome DevTools:
When I open the dummy index.html, I cannot see the React App component. There are no errors on the console as well.
I also tried to add breakpoints in the App component and they are triggered when the page loads. I am just wondering where the index.html page is blank even if I inserted a div with the valid ID.
Here is my App.tsx file:
import React, {Component} from 'react';
import '#progress/kendo-theme-default/dist/all.css';
import './App.css';
import jobs from './jobs.json';
import { Grid, GridColumn } from '#progress/kendo-react-grid';
import { DropDownListChangeEvent } from '#progress/kendo-react-dropdowns';
import EditIconComponent from "./components/editicon.component";
import UploadIconComponent from "./components/uploadicon.component";
export default class App extends Component {
handleDropDownChange = (e: DropDownListChangeEvent) => {
this.setState({
dropdownlistCategory: e.target.value.CategoryID
});
}
state = { skip: 0, take: 5 }
pageChange = (event: any) => {
this.setState({
skip: event.page.skip,
take: event.page.take
});
}
render() {
return (
<div className="App">
<Grid
resizable={true}
pageable={true}
skip={this.state.skip}
take={this.state.take}
total={jobs.length}
onPageChange={this.pageChange}
data={jobs.slice(this.state.skip, this.state.take + this.state.skip)}>
<GridColumn field="null" title=" " cell={EditIconComponent} resizable={false} width="50px"/>
<GridColumn field="null" title=" " cell={UploadIconComponent} resizable={false} width="50px"/>
<GridColumn field="technology" title="Technology" />
<GridColumn field="marketcost" title="Market/Cost"/>
<GridColumn field="num" title="Job #" />
<GridColumn field="locationCode" title="Location Code" />
<GridColumn field="siteName" title="Site Name" />
<GridColumn field="pin" title="PIN" />
<GridColumn field="status" title="Job Status" />
</Grid>
</div>
);
}
}
Here is the entire index.tsx file:
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('scheduleSitesGrid2'));
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: .....
serviceWorker.unregister();
And here is my package.json
{
"name": "kendo-grid-test",
"version": "0.1.0",
"private": true,
"dependencies": {
"#progress/kendo-data-query": "^1.5.2",
"#progress/kendo-drawing": "^1.6.0",
"#progress/kendo-react-dateinputs": "^3.6.0",
"#progress/kendo-react-dialogs": "^3.6.0",
"#progress/kendo-react-dropdowns": "^3.6.0",
"#progress/kendo-react-grid": "^3.6.0",
"#progress/kendo-react-inputs": "^3.6.0",
"#progress/kendo-react-intl": "^3.6.0",
"#progress/kendo-theme-default": "^4.5.2",
"#types/jest": "24.0.19",
"#types/node": "12.11.6",
"#types/react-dom": "16.9.2",
"react": "^16.11.0",
"react-dom": "^16.11.0",
"react-scripts": "3.2.0"
},
"scripts": {
"start": "react-scripts start",
"build": "./node_modules/.bin/webpack",
"pack": "webpack --config webpack.config.js",
"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": {
"#babel/core": "^7.6.4",
"#babel/preset-env": "^7.6.3",
"#babel/preset-react": "^7.6.3",
"#types/react": "^16.9.9",
"babel-loader": "^8.0.6",
"glob": "^7.1.5",
"source-map-loader": "^0.2.4",
"ts-loader": "^6.2.1",
"typescript": "^3.6.4",
"uglifyjs-webpack-plugin": "^2.2.0",
"webpack-cli": "^3.3.10"
}
}
UPDATE
When I created the bundle, public/index.html was an empty file. If I add some basic html code and include the div with id scheduleSitesGrid2, I see that App component is working fine:
Thank you!
I found the issue. I had an issue in my Webpack.config.js file. I set the entry to src/App.tsx, but I changed it to src/index.tsx.
Thank you so much everyone for your help!
Have a good day
Here is my Webpack.config if someone needs it:
"use strict"
const path = require('path');
module.exports = {
entry: './src/index.tsx',
output: {
filename: "bundle.js",
path: path.resolve(__dirname, 'dist')
},
devServer: {
publicPath: "/",
contentBase: "./public",
hot: true
},
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
devtool: "inline-source-map",
module: {
rules: [{
test: /\.(ts|tsx)$/,
use: 'ts-loader',
exclude: /node_modules/
},
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
}]
}
};

Uncaught ReferenceError: ReactDOM is not defined while deplying reactjs app

I'm new to react and I've finished my project so now when I built the app using yarn build command I got this error while using serve.js web server.
I've deleted node_modules and installed them but no result aquired
Uncaught ReferenceError: ReactDOM is not defined
at Object. (bundle.js:1)
this the package.json file content
{
"name": "behtabweb",
"version": "1.0.2",
"description": "",
"main": "index.js",
"scripts": {
"start": "node server.js",
"stop": "pkill --signal SIGINT myApp",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"author": "alireza",
"license": "MIT",
"dependencies": {
"#material-ui/core": "^3.9.2",
"#material-ui/icons": "^3.0.2",
"#react-pdf/renderer": "^1.6.0",
"bootstrap": "^4.3.1",
"brfs": "^2.0.2",
"chart.js": "^2.8.0",
"cross-spawn": "^6.0.5",
"downloadjs": "^1.4.7",
"jspanel4": "^4.6.0",
"material-table": "^1.32.0",
"material-ui-image": "^3.2.0",
"notistack": "^0.6.1",
"ol": "^5.3.1",
"pdfkit": "^0.9.1",
"react": "^16.8.6",
"react-bootstrap": "^1.0.0-beta.5",
"react-chartjs-2": "^2.7.6",
"react-dom": "^16.7.0",
"react-jss": "^8.6.1",
"react-localize-redux": "^3.5.2",
"react-router": "^5.0.0",
"react-router-dom": "^5.0.0",
"react-scripts": "^3.0.0",
"react-swipeable-bottom-sheet": "^1.1.1",
"save-dev": "^2.0.0",
"unicode-properties": "^1.1.0",
"universal-cookie": "^3.0.7",
"vazir-font": "^19.2.0"
},
"devDependencies": {
"#babel/core": "^7.4.5",
"#babel/plugin-proposal-class-properties": "^7.4.4",
"#babel/preset-env": "^7.4.5",
"#babel/preset-react": "^7.0.0",
"autoprefixer-loader": "^3.2.0",
"babel-loader": "^8.0.5",
"babel-preset-es2015": "^6.24.1",
"babel-preset-latest": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"css-loader": "^2.1.1",
"file-loader": "^3.0.1",
"html-webpack-plugin": "^3.2.0",
"json-loader": "^0.5.7",
"less-loader": "^4.1.0",
"react-to-print": "^2.1.2",
"style-loader": "^0.23.1",
"transform-loader": "^0.2.4",
"url-loader": "^1.1.2",
"webpack": "^4.29.6",
"webpack-cli": "^3.2.1",
"webpack-dev-server": "^3.2.1"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
and this the webpack config file content
var webpaqck = require("webpack");
module.exports = {
resolve: {
alias: {
fs: 'pdfkit/js/virtual-fs.js'
}
},
entry: "./src/index.js",
output: {
path: __dirname + "/public/assets",
filename: "bundle.js",
publicPath: "assets"
},
devServer: {
inline: true,
contentBase: "./public",
port: 3003
},
// mode: 'development',
mode: 'production',
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules)/,
use: {
loader: "babel-loader",
options: {
presets: ['#babel/preset-env', {
"targets": {
"node": "current"
}
}],
presets: ["#babel/preset-env", "#babel/preset-react"]
}
}
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
}, {
test: /\.less$/,
use: ['style-loader',
'css-loader',
'less-loader']
},
{
test: /\.(png|jp(e*)g|svg)$/,
use: [{
loader: 'url-loader',
options: {
limit: 8000, // Convert images < 8kb to base64 strings
name: 'images/[hash]-[name].[ext]'
}
}]
}
// ,
// {
// test:/\.json$/,
// exclude:/(node-modules)/,
// use:{
// loader:"json-loader"
// }
// }
]
}
}
this is the index.js file
import React from 'react'
import ReactDOM from 'react-dom'
import { render } from 'react-dom'
import { LocalizeProvider, Translate, withLocalize } from "react- localize-redux";
import { BrowserRouter as Router, Route, Link } from 'react-router-dom'
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
import SignIn from './pages/SignIn'
import { SnackbarProvider } from 'notistack';
import Button from '#material-ui/core/Button';
import Login from './pages/login'
import whoops404 from './components/whoops404'
import dashboard from './pages/Dashboard'
import { MuiThemeProvider, createMuiTheme } from '#material-ui/core/styles';
import white from '#material-ui/core/colors/yellow'
window.React = React.def
const themeRtl = createMuiTheme({
direction: 'rtl',
typography: {
fontFamily: 'Vazir, sans-serif',
useNextVariants: true,
}
});
const App = props => (
<MuiThemeProvider theme={themeRtl}>
<LocalizeProvider>
<SnackbarProvider maxSnack={3}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'left',
}}
action={[
<Button size="small">
<Translate id="dismiss" />
</Button>
]}>
<Router >
<div>
<Route path="/login" component={SignIn}></Route>
<Route exact path="/" component={dashboard}></Route>
</div>
</Router>
</SnackbarProvider>
</LocalizeProvider>
</MuiThemeProvider>
);
render(
<App />,
document.getElementById('react-continer')
)
the app works in development by webpack-dev-server but not in the deployment.

Resources