Why does neither one of my texts get displayed - html

The code down below shows my work in react, where i freshly startet my project and i am still pretty new in this kind of area. I have one simple question that i am just cant comprehend. Its probalby something simple. I expected the h1 with the text Hi too get rendered or to get foo as output, but my website isnt showing any text at all
App.js
import './App.css';
import Navbar from './Components/Navbar';
import {BrowserRouter as Router, Routes, Route } from 'react-router-dom'
function App() {
return (
<div className='App'>
<h1> Hi </h1>
<>
<Router>
<Navbar/>
<Routes>
<Route path='/' exact/>
</Routes>
</Router>
</>
</div>
);
}
export default App;
Navbar.js
import './Navbar.css';
import React, {useState} from 'react'
import { Link } from 'react-router-dom';
function Navbar() {
return (
<nav className='navbar'>
<div className='navbar-container'>
<Link to="/" >foo</Link>
</div>
</nav>
)
}
export default Navbar;
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import reportWebVitals from './reportWebVitals';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
After further testing it has shown that when i delete everything in the return function of App.js except the h1 Hi gets rendered.

I am gussing probably this is the issue. You are using React <18 and trying to use createRoot which is not there.
Change
//Don't use below because it's in React 18
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);
to
let rootElement = document.getElementById('root');
ReactDOM.render(<App />, rootElement);
Here is working example https://stackblitz.com/edit/github-pagkyw

The code is working properly now. The issue was that i installed react-router-dom one folder above the normal rreact app so that i had two seperate package.json files. I've uninstalled react-router-dom and reinstalled it in the same folder of the react folder. The json files got merged and then it worked after reloading

Related

Local page links (<a href='#' />) aren't working with HashRouter

I wanted to add multiple pages to my React website so I started using the HashRouter import from react-router-dom. Since then only my main page loads and I am no longer able to use local links in the page. I can't redirect the user to specific areas on the main page which i used to be able to do before I started using the Router import. This website also uses github pages if that affects anything.
I am currently using the <a> tags like this:
<a className="nav-link" href="/#about-me">
About Me
</a>
with the URL appearing as this with no content below it
http://localhost:3000/#about-me
I have also tried using the <Link> tag but it just ends up reloading the page.
<Link className="nav-link" to="/#about-me">
About Me
</Link>
With this URL appearing instead:
http://localhost:3000/#/#about-me
How do I get my page to scroll down to the id rather than reload or load a blank page?
Main code snippets for reference:
Home.js snippet
const Home = () => {
return (
<div className='main'>
<section className='section-welcome'>
<Introduction />
</section>
<section id='about-me' className='section-about-me'>
<AboutMe />
</section>
</div>
);
};
export default Home;
Main.js
import React from 'react';
import { Routes, Route } from 'react-router-dom';
import Home from './Pages/Home.js';
import NoPage from './Pages/NoPage.js';
const Main = () => {
return (
<Routes>
<Route path='/' element={<Home />}></Route>
<Route path="*" element={<NoPage />} />
</Routes>
);
}
export default Main;
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import { HashRouter as Router } from 'react-router-dom';
import './index.css';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<Router>
<App /> {/* The various pages will be displayed by the `Main` component. */}
</Router>
</React.StrictMode>
);
The problem is with how the code runs with GitHub pages, not the references.
Currently, whenever a link is clicked in the NavBar the website tries to load the URL as root/{href from <a> tag}. This conflicts with GitHub pages as it searches links under the githubname.github.io/project-name/ and the program is trying to display githubname.github.io/#.
In order to fix this, you need to add a basename in the <Router> tag which forces the page to load at /project-name/#.
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import { HashRouter as Router } from 'react-router-dom';
import './index.css';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<Router basename={process.env.PUBLIC_URL}>
<App /> {/* The various pages will be displayed by the `Main` component. */}
</Router>
</React.StrictMode>
);
Relevant Links for more info
https://maximorlov.com/deploying-to-github-pages-dont-forget-to-fix-your-links/
https://create-react-app.dev/docs/deployment/#building-for-relative-paths

Github Pages is not loading subpage even though url is correct

I am creating a multipage website using React and hosting it on Github pages. The website loads perfectly fine on localhost, but runs into trouble on Github pages when trying to enter a subpage.
The main page displays on Github Pages, but when the subpage link is clicked, Github pages displays the error 404 page.
404
File not found
The site configured at this address does not contain the requested file.
Although this error is shown the URL displays the same way the local host displays it.
Main Page
LocalHost: http://localhost:3000/my-website/ ** Runs Fine **
Github: https://myname.github.io/my-website/ ** Runs Fine **
Sub Page
LocalHost: http://localhost:3000/my-website/project ** Runs Fine **
Github: https://myname.github.io/my-website/project ** Runs into Error **
Relevant Code:
Home.js snippet
<div>
<a href='/my-website/project'>
<img alt='Project 1' src={Sunset} />
</a>
</div>
Main.js
import React from 'react';
import { Routes, Route } from 'react-router-dom';
import Home from './Pages/Home.js';
import Project from './Pages/Project.js';
import NoPage from './Pages/NoPage.js';
const Main = () => {
return (
<Routes>
<Route path='/' element={<Home />}></Route>
<Route path='/project' element={<Project />}></Route>
<Route path="*" element={<NoPage />} />
</Routes>
);
}
export default Main;
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter as Router } from 'react-router-dom';
import './index.css';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<Router basename={`/${process.env.PUBLIC_URL}`}>
<App /> {/* The various pages will be displayed by the `Main` component. */}
</Router>
</React.StrictMode>
);
Issues
Assuming your package.json file specifies a correct homepage entry, i.e. "homepage": "https://myname.github.io/my-website", there are a couple issues.
Github pages don't work well with the BrowserRouter. Use the HashRouter instead.
Import and use the Link component to link to pages within the app. Using a raw anchor tag is likely sending a page request to the server for a sub-route that doesn't exist. (This is why the HashRouter is necessary, BTW)
Updates
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import { HashRouter as Router } from 'react-router-dom';
import './index.css';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<Router>
<App />
</Router>
</React.StrictMode>
);
home.js
Replace the raw anchor tag and import and use the Link component that links to the "/project" route path.
import { Link } from 'react-router-dom';
...
<div>
<Link to='/project'>
<img alt='Project 1' src={Sunset} />
</Link>
</div>
The solution was best described in this post in addition to doing what Drew Reese has commented.
Use anchors with react-router
In summary:
You need to install react-router-hash-link.
npm install --save react-router-hash-link
Add import { HashLink as Link } from 'react-router-hash-link'; to the page you use the links.
And use it as shown:
<Link className="nav-link" to="/#about-me">
About Me
</Link>

React-router-dom: Very simple nested routing does not work

I have searched through different tutorials and multiple stackOverflow questions. And none of which, helped me solve a very basic problem:
Implement nested routes with react-router-dom
Here's my code so far:
App.js
<Route exact path="/home" name="Home" component={DefaultLayout} />
DefaultLayout.js
<Route path="/home/users" component={Users} />
When I go to /home/users, I get a blank screen because react-router-dom is looking-up the definition of that route inside App.js instead of searching it inside DefaultLayout.js..
So I have two questions:
QUESION 1: What am I doing wrong exactly?
QUESTION 2: How does react-router-dom know that it should look for the nested route inside DefaultLayout.js instead of inside App.js?
It has been two days and I still cannot solve this simple problem.
Any help is very much appreciated.
EDIT 1: I have started a new project just for the sake of implementing a very simple nested routing:
App.js
import React from "react";
import { BrowserRouter, Switch, Route } from "react-router-dom";
import ParentComponent from "./nestedComponents/ParentComponent";
function App() {
return (
<div>
<BrowserRouter>
<Switch>
<Route exact path="/home" name="Home" component={ParentComponent} />
</Switch>
</BrowserRouter>
</div>
);
}
export default App;
ParentComponent.js
import React from "react";
import nestedComponentOne from "./nestedComponentOne";
import nestedComponentTwo from "./nestedComponentTwo";
import { Switch, Route } from "react-router-dom";
export default function ParentComponent() {
return (
<div>
PARENT COMPONENT
<Switch>
<Route path="home/nestedComponentOne" component={nestedComponentOne} />
<Route path="home/nestedComponentTwo" component={nestedComponentTwo} />
</Switch>
</div>
);
}
nestedComponentOne.js
import React from "react";
export default function nestedComponentOne() {
return <div>NESTED COMPONENT 1</div>;
}
nestedComponentTwo.js
import React from "react";
export default function nestedComponentTwo() {
return <div>NESTED COMPONENT 2</div>;
}
But, I still get a blank screen whenever I try to access a nested component...
You have this problem:
React-router urls don't work when refreshing or writing manually
The simplest fix is to replace the BrowserRouter with a HashRouter

Webpack HMR reloading entire app in some routes

First of all, I'm using:
"react-router": "3.0.2",
browser-sync": "2.18.8",
"url-loader": "0.5.7",
"webpack": "1.13.1",
"webpack-dev-middleware": "1.6.1",
"webpack-hot-middleware": "2.12.2",
"material-ui": "0.17.0",
Ok, now Webpack HMR is working only when I edit reactjs components that are part of the main route, I mean: myurlapp.com/
But if a component is part from another route, example: myurlapp.com/form then the entire app is reloaded. Why is that?
My root js is this:
import React from 'react';
import { render } from 'react-dom';
import { Router, browserHistory } from 'react-router';
import routes from './routes';
import injectTapEventPlugin from 'react-tap-event-plugin';
require('./favicon.ico');
import './styles.scss';
import 'font-awesome/css/font-awesome.css';
import 'flexboxgrid/css/flexboxgrid.css';
injectTapEventPlugin();
render(
<Router routes={routes} history={browserHistory} />, document.getElementById('app')
);
routes.js is this:
import React from 'react';
import { Route, IndexRoute } from 'react-router';
import App from './containers/App';
import NotFoundPage from './containers/NotFoundPage.js';
import LoginPage from './containers/LoginPage';
import FormPage from './containers/FormPage';
import TablePage from './containers/TablePage';
import Dashboard from './containers/DashboardPage';
export default (
<Route>
<Route path="login" component={LoginPage}/>
<Route path="/" component={App}>
<IndexRoute component={Dashboard}/>
<Route path="dashboard" component={Dashboard}/>
<Route path="form" component={FormPage}/>
<Route path="table" component={TablePage}/>
<Route path="*" component={NotFoundPage}/>
</Route>
</Route>
);
And by the way, this is the entries of webpack:
entry: [
'./src/webpack-public-path',
'webpack-hot-middleware/client?reload=true',
'./src/index'
],
What other code should I show? This is what I see in the console of Chrome Dev Tools:
as you can see, I edited the code of Header.js and the update was made ok (without reloading the entire app) but if I edit another component, someone who is exclusive for a router different than the main / then the entire app is realoaded.

<noscript> when using react-router v2 with browser history

I tried to use the browserHistory to make the clean URLs, but when I access the web page, the page is blank and in my console I got <'noscript'> tag. I am using react-router v2.
import React from 'react';
import ReactDOM, { render } from 'react-dom';
import App from './components/App';
import InputForm from './components/InputForm';
import FilterSummary from './components/FilterSummary';
import { Router, Route, browserHistory, IndexRoute } from 'react-router';
render((
<Router history={browserHistory}>
<Route path='/' component={App}>
<IndexRoute component={InputForm} />
<Route path='/filter' component={FilterSummary} />
</Route>
</Router>
), document.getElementById('app'))
Server is WebLogic, so I am not sure how to configure it, as the example only shows it in Express JS. Any idea how to solve this?
EDIT:
Adding screenshot of the HTML response
All JSX is not rendered