I set up a new react project with "npx create-react-app name". I tried to clean all of the files I didn't need, except for the ones that were necessary of course, but now nothing displays. I've had trouble with react-router and routes when it didn't work too.
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>KassaSysteem</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
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')
);
App.js
import './App.css';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom'
import Login from './pages/Login';
function App() {
return (
<Router>
<div className='App'>
<Routes>
<Route path="/" exact component={Home}/>
<Route path="/login" component={Login}/>
</Routes>
</div>
</Router>
);
}
const Home = () => {
<form>
<input type="email" placeholder='your email here'/>
<input placeholder='displayname'/>
<input type="password" placeholder='your password here'/>
</form>
}
export default App;
Login.js
import React from 'react'
function Login() {
return (
<div>
<h1>login</h1>
</div>
)
}
export default Login
These are all of the files I have, except for the CSS files. If anyone knows what I am doing wrong, please comment.
CodePudding user response:
if you use the latest version (v6) you use this code
import './App.css';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom'
import Login from './pages/Login';
function App() {
return (
<Router>
<div className='App'>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/login" element={<Login />}/>
</Routes>
</div>
</Router>
);
}
function Home () {
return (
<form>
<input type="email" placeholder='your email here'/>
<input placeholder='displayname'/>
<input type="password" placeholder='your password here'/>
</form>
)
}
export default App;
CodePudding user response:
install react-router-dom old version that is 5.2.0,
or you can run npm i [email protected] and restart your server that means npm start and its worked and Your home component should be like this.
const Home = () => {
return(
<form>
<input type="email" placeholder='your email here'/>
<input placeholder='displayname'/>
<input type="password" placeholder='your password here'/>
</form>
)
}
CodePudding user response:
On home component you have missed to return and i have tried to give only component it s worked.. And routing is still not working..
const Home = () => {
return(
<form>
<input type="email" placeholder='your email here'/>
<input placeholder='displayname'/>
<input type="password" placeholder='your password here'/>
</form>
)
}