React Router: How do I display only the component content without App.js content when clicking on the Navbar button?

huangapple go评论114阅读模式
英文:

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 &quot;/login&quot;, 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 &#39;react&#39;;
import { BrowserRouter as Router, Route, Link, Routes, Outlet } from &#39;react-router-dom&#39;;
import About from &#39;./AboutPage/About&#39;;
import Login from &#39;./Login&#39;;
import CA from &#39;./CA&#39;
import &#39;./App.css&#39;
import `gifImage` from &#39;./output-`onlinegiftools`.gif&#39;;


`function` App() {
  return (
    &lt;div ``className``=&#39;App&#39;&gt;
    &lt;Router&gt;
            &lt;div `className`=&#39;presentation&#39;&gt;
            &lt;nav&gt;
                &lt;Link to=&#39;/About&#39; `your text`className=&#39;link&#39;&gt;&lt;h1&gt;app&lt;/h1&gt;&lt;/Link&gt;
                &lt;div `className`=&#39;login&#39;&gt;
                &lt;Link to=&quot;/CA&quot; className=&quot;link&quot;&gt;&lt;p&gt;Create Account&lt;/p&gt;&lt;/Link&gt;
                &lt;Link to=&quot;/Login&quot; className=&quot;link&quot;&gt;&lt;button&gt;Login Now&lt;/button&gt;&lt;/Link&gt;
                &lt;Outlet /&gt;
                &lt;/div&gt;
            &lt;/nav&gt;
            &lt;h1 className=&#39;h11&#39;&gt;********* &lt;br /&gt; ********&lt;/h1&gt;
            &lt;div className=&quot;GIF&quot;&gt;
              &lt;img src={gifImage} alt=&quot;Example GIF&quot; /&gt;
            &lt;/div&gt;
            &lt;/div&gt;

            &lt;div className=&#39;part2&#39;&gt;
            &lt;p class=&quot;new&quot;&gt;New Features Available&lt;/p&gt;
            &lt;h1&gt;**************&lt;/h1&gt;
            &lt;p&gt;******************************************************** &lt;br /&gt; **************************** &lt;br /&gt;******************** &lt;br /&gt; **********..&lt;/p&gt;
            &lt;button&gt;Login for More&lt;/button&gt;
            &lt;/div&gt;

            &lt;Routes&gt;
                &lt;Route path=&quot;/About&quot; element={&lt;About /&gt;} /&gt;
                &lt;Route path=&quot;/Login&quot; element={&lt;Login /&gt;} /&gt;
                &lt;Route path=&quot;/CA&quot; element={&lt;CA render={true} /&gt;} /&gt;
            &lt;/Routes&gt;
        &lt;/Router&gt;
    &lt;/div&gt;
  );
}


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 &lt;&gt;
		&lt;h1 className=&#39;h11&#39;&gt;********* &lt;br /&gt; ********&lt;/h1&gt;
		&lt;div className=&quot;GIF&quot;&gt;
		  &lt;img src={gifImage} alt=&quot;Example GIF&quot; /&gt;
		&lt;/div&gt;
		{ /* move the code that should not be shown on every page here... */ }
	&lt;/&gt;;
}

App.js:

function App() {
	// ...
	return (
		// other elements... (keep the common elements here)
		&lt;Routes&gt;
			&lt;Route path=&quot;/&quot; element={&lt;Home /&gt;} /&gt;
			&lt;Route path=&quot;/About&quot; element={&lt;About /&gt;} /&gt;
			&lt;Route path=&quot;/Login&quot; element={&lt;Login /&gt;} /&gt;
			&lt;Route path=&quot;/CA&quot; element={&lt;CA render={true} /&gt;} /&gt;
		&lt;/Routes&gt;
	);
}

</details>



huangapple
  • 本文由 发表于 2023年5月29日 07:38:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定