使用useNavigate将div添加到页面而不是导航?

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

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 = () =&gt; {
  return &lt;div&gt;About&lt;/div&gt;;
};

Main Page:

import { useNavigate, Route, Routes } from &quot;react-router&quot;;
import { About } from &quot;./About&quot;;
type ButtonProps = {
  text: string,
  onClicked: Function,
};

function Button({ text, onClicked }: ButtonProps) {
  return (
    &lt;button
      className=&quot;px-5 border-sky-700 border-2 text-sky-600 rounded-full transform hover:bg-gray-800 hover:scale-125 transition duration-200&quot;
      onClick={() =&gt; onClicked()}
    &gt;
      {text}
    &lt;/button&gt;
  );
}

function Nav() {
  let navigate = useNavigate();
  function navPath(path: string) {
    navigate(path);
  }
  return (
    &lt;div className=&quot;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&quot;&gt;
      &lt;div className=&quot;w-50 items-center flex&quot;&gt;
        &lt;Button text=&quot;Who Am I&quot; onClicked={() =&gt; navigate(&quot;./About&quot;)} /&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
}

function App() {
  return (
    &lt;main&gt;
      //a few divs
      &lt;Routes&gt;
        &lt;Route path=&quot;/About&quot; element={&lt;About /&gt;} /&gt;
      &lt;/Routes&gt;
    &lt;/main&gt;
  );
}
export default App;

Index.tsx

import React from &quot;react&quot;;
import ReactDOM from &quot;react-dom/client&quot;;
import &quot;./index.css&quot;;
import App from &quot;./pages/App&quot;;
import reportWebVitals from &quot;./reportWebVitals&quot;;
import { BrowserRouter } from &quot;react-router-dom&quot;;

const root = ReactDOM.createRoot(document.getElementById(&quot;root&quot;) as HTMLElement);
root.render(
  &lt;BrowserRouter&gt;
    &lt;App /&gt;
  &lt;/BrowserRouter&gt;
);
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会被显示出来,因为它们被返回,不依赖于&lt;Routes&gt;组件。您可能希望为您称之为主页的divs创建一个路由:

&lt;main&gt;
  &lt;Routes&gt;
     &lt;Route path = &quot;/&quot; element={&lt;MainPage/&gt;}/&gt;
  &lt;/Routes&gt;   
  &lt;Routes&gt;
     &lt;Route path = &quot;/About&quot; element={&lt;About/&gt;}/&gt;
  &lt;/Routes&gt;
&lt;/main&gt;
function MainPage(){
  //...
  let navigate = useNavigate()
  function navPath(path:string){
    navigate(path)
  }
  return (
    &lt;&gt;
    // 几个divs 
    // 几个divs 
    &lt;div className = &quot;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&quot;&gt;
        &lt;div className = &quot;w-50 items-center flex&quot;&gt;
        &lt;Button text=&quot;Who Am I&quot; onClicked={()=&gt;navigate(&quot;/About&quot;)}/&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;/&gt;
  )
}

注意: navigate(&quot;/About&quot;) 不需要 . <br> 您只需指定路径。

英文:

//a few divs will be displayed anyway since they are returned and do not depend on the &lt;Routes&gt; component. you may want to create a route for your divs as you call them main page:

&lt;main&gt;
  &lt;Routes&gt;
     &lt;Route path = &quot;/&quot; element={&lt;MainPage/&gt;}/&gt;
  &lt;/Routes&gt;   
  &lt;Routes&gt;
     &lt;Route path = &quot;/About&quot; element={&lt;About/&gt;}/&gt;
  &lt;/Routes&gt;
&lt;/main&gt;
function MainPage(){
  //...
  let navigate = useNavigate()
  function navPath(path:string){
    navigate(path)
  }
  return (
    &lt;&gt;
    // few divs 
    // few divs 
    &lt;div className = &quot;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&quot;&gt;
        &lt;div className = &quot;w-50 items-center flex&quot;&gt;
        &lt;Button text=&quot;Who Am I&quot; onClicked={()=&gt;navigate(&quot;/About&quot;)}/&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;/&gt;
  )
}

Note: navigate(&quot;/About&quot;) without .<br> you just specify the path

huangapple
  • 本文由 发表于 2023年7月11日 09:34:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658223.html
匿名

发表评论

匿名网友

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

确定