英文:
React Router: How do I display only the component content without App.js content when clicking on the Navbar button?
问题
我在我的页面中遇到了一个问题。
我有一个包含按钮的导航栏,应该使用路由将我带到登录页面,但当我点击它时,它确实带我到"/login"
,但登录组件的内容添加到了App.js的内容中。相反,我只希望显示登录组件的内容。
这是App.js的代码:
import React from 'react';
import { BrowserRouter as Router, Route, Link, Routes, Outlet } from 'react-router-dom';
import About from './AboutPage/About';
import Login from './Login';
import CA from './CA';
import './App.css';
import gifImage from './output-onlinegiftools.gif';
function App() {
return (
<div className='App'>
<Router>
<div className='presentation'>
<nav>
<Link to='/About' className='link'><h1>app</h1></Link>
<div className='login'>
<Link to="/CA" className="link"><p>Create Account</p></Link>
<Link to="/Login" className="link"><button>Login Now</button></Link>
<Outlet />
</div>
</nav>
<h1 className='h11'>********* <br /> ********</h1>
<div className="GIF">
<img src={gifImage} alt="Example GIF" />
</div>
</div>
<div className='part2'>
<p className="new">New Features Available</p>
<h1>**************</h1>
<p>******************************************************** <br /> **************************** <br />******************** <br /> **********..</p>
<button>Login for More</button>
</div>
<Routes>
<Route path="/About" element={<About />} />
<Route path="/Login" element={<Login />} />
<Route path="/CA" element={<CA render={true} />} />
</Routes>
</Router>
</div>
);
}
export default App;
当我点击导航栏时,我希望只显示登录和CA(创建帐户)组件的内容,但实际上显示了App.js的内容。
英文:
I have a problem in my page.
I have a navbar containing a button that should take me to login page using router, but when I click on it, it did take me to "/login"
, but the content of login component adds to the content of App.js. Instead, I want just the content of the login component to show.
This is the App.js code :
import React from 'react';
import { BrowserRouter as Router, Route, Link, Routes, Outlet } from 'react-router-dom';
import About from './AboutPage/About';
import Login from './Login';
import CA from './CA'
import './App.css'
import `gifImage` from './output-`onlinegiftools`.gif';
`function` App() {
return (
<div ``className``='App'>
<Router>
<div `className`='presentation'>
<nav>
<Link to='/About' `your text`className='link'><h1>app</h1></Link>
<div `className`='login'>
<Link to="/CA" className="link"><p>Create Account</p></Link>
<Link to="/Login" className="link"><button>Login Now</button></Link>
<Outlet />
</div>
</nav>
<h1 className='h11'>********* <br /> ********</h1>
<div className="GIF">
<img src={gifImage} alt="Example GIF" />
</div>
</div>
<div className='part2'>
<p class="new">New Features Available</p>
<h1>**************</h1>
<p>******************************************************** <br /> **************************** <br />******************** <br /> **********..</p>
<button>Login for More</button>
</div>
<Routes>
<Route path="/About" element={<About />} />
<Route path="/Login" element={<Login />} />
<Route path="/CA" element={<CA render={true} />} />
</Routes>
</Router>
</div>
);
}
export default App;
I want to display only the content of login and CA (create account) component when clicking on the navbar, but when I, do the content of App.js get displayed too
答案1
得分: 2
将要在首页显示的内容提取到一个单独的组件中,并且只在路径为 /
时渲染该内容。
Home.js:
export default function Home() {
return (
<>
<h1 className='h11'>********* <br /> ********</h1>
<div className="GIF">
<img src={gifImage} alt="Example GIF" />
</div>
{ /* 将不应该在每个页面上显示的代码移至这里... */ }
</>
);
}
App.js:
function App() {
// ...
return (
// 其他元素...(将通用元素保留在这里)
<Routes>
<Route path="/" element={<Home />} />
<Route path="/About" element={<About />} />
<Route path="/Login" element={<Login />} />
<Route path="/CA" element={<CA render={true} />} />
</Routes>
);
}
英文:
Extract the content to be shown on the home page to a separate component and render that only for the path /
.
Home.js:
export default function Home() {
return <>
<h1 className='h11'>********* <br /> ********</h1>
<div className="GIF">
<img src={gifImage} alt="Example GIF" />
</div>
{ /* move the code that should not be shown on every page here... */ }
</>;
}
App.js:
function App() {
// ...
return (
// other elements... (keep the common elements here)
<Routes>
<Route path="/" element={<Home />} />
<Route path="/About" element={<About />} />
<Route path="/Login" element={<Login />} />
<Route path="/CA" element={<CA render={true} />} />
</Routes>
);
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论