Why does my React Website Take 43 seconds to Load? - node.js

I'm not sure why my react website is taking so long to load. It takes 43 seconds
All I have is in index.jsx
import ReactDOM from "react-dom";
import React from "react";
import { HashRouter, Route } from "react-router-dom";
import Home from "./components/Home";
ReactDOM.render(
<HashRouter>
<div>
<Route exact path="/" component={Home} />
</div>
</HashRouter>,
document.getElementById("main")
);
Home.jsx: imports react and renders hi
webpack.config.js : https://pastebin.com/raw/zdUws0R8
package.json : https://pastebin.com/raw/VR6pSP44
index.html : https://pastebin.com/raw/9AVNBpTN

I checked your website and it seems to be working fine to me for now;
For more details, I have added a:
Website Request screenshot
You might want to have a look at your SSL certificate though.
All the best!

I think you need to reinstall your project via :
npx create-react-app YourProject
and use
BrowserRouter
instead of
HashRouter
in 'react-router-dom'
then start the development server after creating the components or editing it via
npm start

Related

react 18 typescript types in package.json

Im trying to upgrade my react 17 to 18
"#types/react": "???????",
"#types/react-dom": "????????",
"react": "^18.0.0",
"react-dom": "^18.0.0",
What should I put in #types/react and #types/react-dom ?
Thanks
I used Create React App to generate a new TypeScript project and take a look at the dependencies, since I was striking out with the recommendation of removing #types/react and #types/react-dom. That was not fun.
I ended up installing these types:
"#types/react": "^17.0.43",
"#types/react-dom": "^17.0.14",
Then I could use the following to mount my app in index.tsx:
import React from 'react';
import { createRoot } from 'react-dom/client';
....
const container = document.getElementById('root');
// OH BOY, container can be null but createRoot can't handle this...
if (!container) {
throw "Can't instantiate";
}
const root = createRoot(container);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
Yes, there is a problem with the current typescript type where it could receive a null, and you have to guard it with an if / throw check... Or do a ts-ignore.
I'm hoping that gets fixed somehow. But anyway, it's 1AM here and I finally was able to get my app working with React 18 and Typescript using this hack.
At the time I write this, the React 18 types don't appear to be finished yet (see tweet from maintainer). There are former "#next" versions of React 18 types, but they don't match the React 18 release and probably shouldn't be used. Sit tight and they'll likely be finished/released in the coming days.
while react team working on the new types release, if you want to test it out you can keep the old "#types/react": "^17.0.40" and "#types/react-dom": "^17.0.13" types version, and inside the index.tsx after updating react and react-dom to version 18.0.0 you need first to import ReactDOM from react-dom/client and then create the root element from the element id=root using createRoot and then use .render to render the content of the <App/> component inside the created root element like this:
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
let root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>,
);
Looks like #types/react and #types/react-dom are now on v18. Here's how I upgraded:
uninstall both those packages
upgrade to react 18
yarn install -D #types/react #types/react-dom, which automatically uses v18 types now. (or npm install --save-dev ...)
I'm sure there's a way that doesn't require you to uninstall first via yarn upgrade, but that didn't work for me for some reason.

React app server wont restart automatically using create react app

I am Making a simple app using create-react-app
but the server stops and won't update with changes
Plz Help
npm version 6.14.4
node version 10.19.0
Ubuntu 20.04 LTS
WSL 2 Windows 10 Home\
My Index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
serviceWorker.unregister();
My app.js
import React from 'react';
import Palette from './Palette';
import seedColors from './seedColors'
import './App.css';
function App() {
return (
<div className ="App">
<Palette colors ={seedColors[3]}/>
</div>
);
}
export default App;
My Palette.js
import React, { Component } from "react";
class Palette extends Component{
render(){
return(
<div><h1>Palette</h1></div>
)
}
}
export default Palette
The seedColors.js ,I have taken from https://github.com/Colt/react-colors/blob/master/colors-app/src/seedColors.js
Try adding a .env file to your root project directory (where you have package.json and node_modules). Then inside .env, add CHOKIDAR_USEPOLLING=true. This just worked for me when I was having issues with hot reloading not working. Apparently the "watcher" requires this setting when working with a VM (like WSL). I found this recommended in the top answer here: React create app hot reload is not always working on linux

Android - Invariant failed: Browser history needs a DOM

I fetch a list of articles from my site backend (nodejs) and print it in the react demo app.
The app works correctly from the Web. When I try to start it in Android I get the following error:
Invariant failed: Browser history needs a DOM
I think the problem is here:
import {BrowserRouter, Router, Link, Route, Switch} from 'react-router-dom';
...
<BrowserRouter>
<Switch>
<Route path="/detail/:id" component={RouterDetail}/>
<Route exact={true} path="/home" render={() => content}/>
</Switch>
</BrowserRouter>
I have read everything on the web about this error but I have not been able to understand the problem.
I found a lot about react server side.
But my case is different, i use react only as a frontend.
I also tried the following change, from web works, on android I encounter the same problem:
import { Router } from "react-router";
import {Link, Switch, Route} from 'react-router-dom';
import createBrowserHistory from 'history/createBrowserHistory';
...
const history = createBrowserHistory();
...
<Router history={history}>
<Route path="/detail/:id" component={RouterDetail}/>
<Route exact={true} path="/home" render={() => props.content}/>
</Router>

React.js website build works on one host, but not another

I am new to react.js . I have created a react website using create-rect-app. I have run: npm run build in git and got my static site files. Everything works on the test host, however when i moved hosts only index page works and navigating to other page gives 404 error. For routing im using react-router-dom.
How can i get my page to work on the other host?
Working host: http://000webhost.com/
Badly working host is some local provider
Edit: Basically i have pages such as /Home and /Contact.
Im using react-router-dom.
code:
import React, { Component } from 'react';
import { BrowserRouter as Router, Route} from 'react-router-dom';
import logo from './logo.svg';
import './App.css';
import Home from './pages/Home.jsx';
import Contacts from './pages/Contacts.jsx';
import Our_products from './pages/Our_products.jsx';
class App extends Component {
render() {
return (
<Router>
<div>
<Route exact path ="/" component={Home}/>
<Route exact path ="/Home" component={Home}/>
<Route exact path ="/Contacts" component={Contacts}/>
<Route exact path ="/Our-products" component={Our_products}/>
</div>
</Router>
);
}
}
export default App;
My index is linked to /Home as you can see from code. i have uploaded Build folder to public_html on both hosting platforms. On one site works normaly, on the other only /Home page shows up.
So, you can ignore this, but what helped is changing
<a href="/" />
to
<Link to="/" />
. Basically one hosting provider could render links as
<a />
other only as
<Link />

Render react component on node server, with webpack

I have a react component. <myFooter>. It is a simple footer.
import React from 'react';
import './my-footer.scss';
export default class myFooter extends React.Component {
render() {
return (
<footer className="col-xs-12">
hello !
</footer>
);
}
}
I want to render it from the server-side. On the backend, I have an express server. For that I wrote this:
import myFooter from '../components/my-footer.jsx';
app.get('/footer', function(req, res) {
var string1 = ReactDOMServer.renderToString(myFooter);
res.send(string1);
});
Now the problem is that server cannot read sass files. For client side rendering, I am using webpack. Webpack builds everything and gives a bundle file.
But i'm not sure what happens if its the server side. How can I compile using webpack. If I can, will i need to compile my app for each request on node server ?
You need 2 webpack builds:
1 to build the client
1 to build the server.
When building the server, use css-loader/locals instead of css-loader in your webpack configuration.
Read through this issue for more details: https://github.com/webpack/css-loader/issues/59

Resources