英文:
useNavigate adding div to page instead of navigating?
问题
关于你的问题,你想要使用React和react-router的useNavigate()函数来实现页面之间的导航,但目前遇到了问题,点击按钮时页面内容不是导航到一个新的空白页面,而是内容附加到主页面上。以下是你的代码,我将提供一些建议:
首先,请确保你使用的是react-router v6版本,因为v6与v5有一些不同之处。
在你的Main Page
组件中,修改按钮的点击事件处理函数如下:
function Button({ text, onClicked }: ButtonProps) {
return (
<button
className="px-5 border-sky-700 border-2 text-sky-600 rounded-full transform hover:bg-gray-800 hover:scale-125 transition duration-200"
onClick={() => onClicked()}
>
{text}
</button>
);
}
然后,在Main Page
组件中,使用useNavigate
来导航到About页面:
function Nav() {
let navigate = useNavigate();
return (
<div className="shadow-xl bg-sky-800 bg-opacity-10 md:w-3/2 lg:w-[60rem] top-10 h-16 flex items-center absolute justify-center space-x-12 z-10 rounded-full overflow-hidden">
<div className="w-50 items-center flex">
<Button text="Who Am I" onClicked={() => navigate("/About")} />
</div>
</div>
);
}
确保你在点击按钮时使用navigate("/About")
来跳转到About页面。这应该会导航到一个新的空白页面,显示About组件的内容。
如果问题仍然存在,请确保你的react-router版本是v6,并检查是否正确引入了相关组件和路由。
希望这能帮助你解决问题!如果还有其他问题,请随时提问。
英文:
I am new to using React to build a website and am trying to navigate to another page using the useNavigate() function provided by react-router. I have a separate file/component called About.tsx that I am trying to navigate to using the onClick property of a button located on the main page of my website. When I click the button however, the contents of my About.tsx page, which is simply a div for now, get tacked on to the side of my main page. The functionality I am looking for is for the button to navigate to a completely new blank page containing the div only. Is this possible using useNavigate() and if so how should I fix this, or do I need to look into other potential solutions. My code is attached below:
About.tsx:
export const About = () => {
return <div>About</div>;
};
Main Page:
import { useNavigate, Route, Routes } from "react-router";
import { About } from "./About";
type ButtonProps = {
text: string,
onClicked: Function,
};
function Button({ text, onClicked }: ButtonProps) {
return (
<button
className="px-5 border-sky-700 border-2 text-sky-600 rounded-full transform hover:bg-gray-800 hover:scale-125 transition duration-200"
onClick={() => onClicked()}
>
{text}
</button>
);
}
function Nav() {
let navigate = useNavigate();
function navPath(path: string) {
navigate(path);
}
return (
<div className="shadow-xl bg-sky-800 bg-opacity-10 md:w-3/2 lg:w-[60rem] top-10 h-16 flex items-center absolute justify-center space-x-12 z-10 rounded-full overflow-hidden">
<div className="w-50 items-center flex">
<Button text="Who Am I" onClicked={() => navigate("./About")} />
</div>
</div>
);
}
function App() {
return (
<main>
//a few divs
<Routes>
<Route path="/About" element={<About />} />
</Routes>
</main>
);
}
export default App;
Index.tsx
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./pages/App";
import reportWebVitals from "./reportWebVitals";
import { BrowserRouter } from "react-router-dom";
const root = ReactDOM.createRoot(document.getElementById("root") as HTMLElement);
root.render(
<BrowserRouter>
<App />
</BrowserRouter>
);
reportWebVitals();
I have tried rewriting the functions as well as reformatting the About page. Other YouTube tutorials and ChatGPT have not helped either. Any assistance would be greatly appreciated!
I am also new to StackOverflow so if I missed anything, please do let me know and I would be happy to update this post! Also my apologies for the poor formatting! Thank you!
答案1
得分: 1
//a few divs
会被显示出来,因为它们被返回,不依赖于<Routes>
组件。您可能希望为您称之为主页的divs创建一个路由:
<main>
<Routes>
<Route path = "/" element={<MainPage/>}/>
</Routes>
<Routes>
<Route path = "/About" element={<About/>}/>
</Routes>
</main>
function MainPage(){
//...
let navigate = useNavigate()
function navPath(path:string){
navigate(path)
}
return (
<>
// 几个divs
// 几个divs
<div className = "shadow-xl bg-sky-800 bg-opacity-10 md:w-3/2 lg:w-[60rem] top-10 h-16 flex items-center absolute justify-center space-x-12 z-10 rounded-full overflow-hidden">
<div className = "w-50 items-center flex">
<Button text="Who Am I" onClicked={()=>navigate("/About")}/>
</div>
</div>
</>
)
}
注意: navigate("/About")
不需要 .
<br> 您只需指定路径。
英文:
//a few divs
will be displayed anyway since they are returned and do not depend on the <Routes>
component. you may want to create a route for your divs as you call them main page:
<main>
<Routes>
<Route path = "/" element={<MainPage/>}/>
</Routes>
<Routes>
<Route path = "/About" element={<About/>}/>
</Routes>
</main>
function MainPage(){
//...
let navigate = useNavigate()
function navPath(path:string){
navigate(path)
}
return (
<>
// few divs
// few divs
<div className = "shadow-xl bg-sky-800 bg-opacity-10 md:w-3/2 lg:w-[60rem] top-10 h-16 flex items-center absolute justify-center space-x-12 z-10 rounded-full overflow-hidden">
<div className = "w-50 items-center flex">
<Button text="Who Am I" onClicked={()=>navigate("/About")}/>
</div>
</div>
</>
)
}
Note: navigate("/About")
without .
<br> you just specify the path
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论