App.js
import './App.css';
import CreateBug from './CreateBug';
import { BrowserRouter as Route, Routes } from 'react-router-dom';
import Button from '@mui/material/Button';
import { useNavigate } from "react-router-dom";
function App(history) {
const navigate = useNavigate();
const handleOnClick = () => navigate('/createBug');
return (
<div className="App">
<Routes>
<Route path="/createBug" element={<CreateBug />} />
</Routes>
<header className="App-header">
<p>
BUGHOUND
</p>
<Button variant="contained" onClick={handleOnClick()}>
Create a bug
</Button>
</header>
</div>
);
}
export default App;
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
reportWebVitals();
On click of that create bug button in app.js, CreateBug
page should be rendered. which is the below code.
CreactBug.js
import React, { useState } from 'react';
function CreateBug() {
const [newBugDescription, setNewBugDescription] = useState;
return (
<div>
New Bug Report Entry Page
<form>
<label htmlFor="newBugDescription">
New Bug Description:
</label>
<input
type="text"
id="newBugDescription"
value={newBugDescription}
onChange={event => setNewBugDescription(event.target.value)}
></input>
</form>
</div>
)
}
export default CreateBug
I’m not able to route to create bug page, please help me with this. I have attached the error screenshot with this.
2
Answers
Issue
You are importing the
BrowserRouter
asRoute
and improperly using it.Solution
Import and router and render it higher in the ReactTree than any component that is trying to access the routing context value it provides.
Import the
BrowserRouter
inindex.js
and wrap theApp
component.index.js
Import
Routes
andRoute
inApp.js
. Don’t immediately invoke thehandleClick
function, i.e. useonClick={handleOnClick}
instead ofonClick={handleOnClick()}
.App.js
The
CreateBug
component also should invoke theuseState
hook, i.e.useState("")
instead of justuseState
.there are 3 problems in ur code
1: while using the onClick property in app.js
u wrote
onClick = {handleOnClick()}
which is incorrect, The correct method is
2:the second problem is
there is no () after the useState in CreateBug.js
3: The third problem is
the incorrect use of browser router
u are suppsoed to wrap all ur divs where u want the routing to take place into a tag and then specify the isndie them
i will attach the correct code below
INDEX.JS
APP.JS
createbug.js remains same as given