My React app showing a blank page on my localhost - reactjs

I am using VSCode to create a react app, I have multiple folders as shown in the first image:
This is my App.jsx
import React from "react";
import Header from "./components/Header";
import Footer from "./components/Footer";
import Note from "./components/Note";
function App() {
return (
<div>
<Header />
<Footer />
<Note />
</div>
);
}
export default App;
This is my Index.jsx
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';
import reportWebVitals from './reportWebVitals';
ReactDOM.render(<App />, document.document.getElementById("root"));
reportWebVitals();
This is my Header.jsx
import React from "react";
function Header() {
return (
<header>
<h1>Keeper</h1>
</header>
);
}
export default Header;
This is my Footer.jsx
import React from "react";
function Footer() {
const date = new Date();
const curretYear = date.getFullYear();
return (
<footer>
<p>copyright ⓒ {curretYear}</p>;
</footer>
);
}
export default Footer;
I am trying to learn react on VSCode, the same code works on Codesandbox, but I can't get it to work on my VSCode, and I am not getting any errors in the terminal. I am learning React and I would like the help.

Your import paths in App.jsx appear to be wrong try ./Header etc. Remember that import paths are relative and since the imported modules are on the same level as App.jsx, you don't need to go through the components folder

Related

Why material ui is not working? It is not rendering in the browser

I was following a project tutorial of Jan 30, 2022.
I have installed material ui using ----legacy-peer-deps and same for icons.
I am then trying to use AppBar and Typography but it is not showing on browser (I mean not rendering. Its completely blank there). How can I solve this issue?
this is my index.js file:
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
This is App.js file:
import Header from "./components/Header";
function App() {
return (
<div>
<Header/>
</div>
);
}
export default App;
this is Header.js:
import React from "react";
import { AppBar, Typography } from "#mui/material";
const Header = () => {
return (
<div>
<AppBar >
<Typography>E-Granthalaya</Typography>
</AppBar>
</div>
);
};
export default Header;

React - Navbar not showing and I am getting a Blank Screen

I am trying to create a Navbar on a clean React app (with React-Router-Dom) and for some reason I am just getting a blank screen, as far as I can see and from looking up various guides, it should be fine:
App.js:
import React from "react";
import Navbar from "./components/Navbar";
function App() {
return (
<Navbar/>
);
}
export default App;
Index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
Navbar.js
import React from "react";
import { Link } from "react-router-dom";
const Navbar = ()=>{
return(
<nav className = "nav-wrapper">
<div className="container">
<Link to="/" className="brand-logo">Potter Books</Link>
<ul className="right">
<li><Link to="/">Shop</Link></li>
<li><Link to="/">Cart</Link></li>
</ul>
</div>
</nav>
);
}
export default Navbar;
There are multiple ways to fix that.
What I mostly like to use BrowserRouter into the App.js
modify your app.js file by importing BrowserRouter
import {
BrowserRouter,
} from "react-router-dom";
And then add this in the App function.
function App() {
return (
<BrowserRouter>
<Navbar/>
<BrowserRouter/>
);
}
export default App;
For more information, you can consult this page as well.
So to fix this you need to add
import {BrowserRouter as Router} from "react-router-dom";
into Index.js

Module not found: Can't resolve header.js or app.js

Console says that it can't find the module in the directory. I've read about relative paths and tried to redirect but it doesn't seem to work. I am trying to import a header into the app.js file and then show that app.js file into the index.js file.
My index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import App from '../components/ui/App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
my app.js:
import React from 'react';
import Header from './components/ui/Header';
function App() {
return (
<div className="App">
<Header />
Hello!
</div>
);
}
export default App;
What am I doing wrong here?
Well, first of all, why not share the screenshot of the project structure.
Second, try this one.
import App from '../components/ui/app.js';
Third, check your header.js to check that it has export default ...

Problem with rendering React component in Laravel project

I have set up React with my Laravel project. In the resources > js folder I have an App.js and an index.js. I also have an Example.js file in resources > js > components folder. What I am trying to do is to import my Example.js into the App.js, then import App.js into the index.js and render it into my blade template. The problem is that it's not showing anything inside of my root div on the page.
Here's the code in my Example.js file:
import React from 'react';
function Example() {
return (
<div>
TEST
</div>
);
}
export default Example;
This is the code I have inside App.js:
import React from "react";
import Example from "./components/Example";
function App() {
return (
<Example/>
)
}
export default App;
And this is index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import App from "./App";
ReactDOM.render(<App/>, document.getElementById("root"));

Bootstrap is not working properly in my reactjs project

First of all, I am truly new in reactjs. I am trying to design my react project using bootstrap. For some reasons it's not working as it should be. Below is my code:
App.js
import React from 'react';
import './App.css';
import Menu from '../components/Menu/Menu';
function App() {
return (
<div className="App">
<Menu />
</div>
);
}
export default App;
Menu.js
import React from 'react'
import * as ReactBootstrap from 'react-bootstrap'
const Menu = () => {
return (
<div id="menu_container">
<ReactBootstrap.Navbar bg="dark" variant="dark">
<ReactBootstrap.Navbar.Brand href="#home">Navbar</ReactBootstrap.Navbar.Brand>
<ReactBootstrap.Nav className="mr-auto">
<ReactBootstrap.Nav.Link href="#home">Home</ReactBootstrap.Nav.Link>
<ReactBootstrap.Nav.Link href="#features">Features</ReactBootstrap.Nav.Link>
<ReactBootstrap.Nav.Link href="#pricing">Pricing</ReactBootstrap.Nav.Link>
</ReactBootstrap.Nav>
<ReactBootstrap.Form inline>
<ReactBootstrap.FormControl type="text" placeholder="Search" className="mr-sm-2" />
<ReactBootstrap.Button variant="outline-light">Search</ReactBootstrap.Button>
</ReactBootstrap.Form>
</ReactBootstrap.Navbar>
</div>
)
}
export default Menu;
Index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './containers/App';
import * as serviceWorker from './serviceWorker';
import 'bootstrap/dist/css/bootstrap.min.css';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
serviceWorker.unregister();
Current Output:
Current output
Output I want:
Output I want
Make sure you add the CDN link in index.html.
Although you found it this would help others.
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"
integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS"
crossorigin="anonymous"/>
or add this in app.css
#import "bootstrap/dist/css/bootstrap.css";
You have some .css files that are imported in App.js and index.js. Maybe they are overriding some bootstrap rules.
I took your code here https://codesandbox.io/s/test-react-bootstrap-75rsd, removed the App.css and index.css imports and it looks just as you want.

Resources