[英]How can i route from one HTML page to another in react js?
I am new to javascript and react, and i am trying to route from one component to another when a button is clicked.我是 javascript 的新手并做出反应,我试图在单击按钮时从一个组件路由到另一个组件。
example of html code in the sign up page:注册页面中的 html 代码示例:
<!-- signup button -->
<div id = "signup">
<button class="signupbutton">Sign up</button>
</div>
so when the sign up button i want it to route to this html page:所以当注册按钮我希望它路由到这个 html 页面:
<!-- page title -->
<h1><strong>Let's get started! First up, where are you in the planning process?</strong</h1>
Any ideas on how i can do this?关于我如何做到这一点的任何想法? - i know i need to do this in javascript and with react (i ahve created a JS file for the sign up page and planning process page), but i am a bit unsure of how to do so. - 我知道我需要在 javascript 和 react 中执行此操作(我已经为注册页面和计划流程页面创建了一个 JS 文件),但我有点不确定如何去做。 Any ideas?有任何想法吗?
You can't link in HTML directly with React.你不能用 React 直接在 HTML 中链接。 You need to set up two components first.您需要先设置两个组件。
One for the page with the Button:一个用于带有按钮的页面:
export default function ButtonPage () {
return (
<div id = "signup">
<button className="signupbutton">Sign up</button>
</div>
);
}
One with the page for the Get Started Page:一个带有入门页面的页面:
export default function GetStarted () {
return (
<h1><strong>Let's get started! First up, where are you in the planning process?</strong</h1>
);
}
Then You need to set up your main component, the App component and link the child components you want to display.然后您需要设置您的主组件、App 组件并链接您要显示的子组件。 If you use the latest version of React you need to import BrowserRouter
, Route
and Routes
from react-router-dom
:如果您使用最新版本的 React,您需要从react-router-dom
导入BrowserRouter
、 Route
和Routes
:
import { BrowserRouter, Route, Routes } from "react-router-dom";
export default function App () {
return (
<BrowserRouter>
<Routes>
<Route path="/signup" element={<ButtonPage/>}></Route>
<Route path="/getstarted" element={<GetStarted/>}></Route>
</Routes>
</BrowserRouter>
);
}
Then you need to import Link
from react-router-dom
inside your ButtonPage Component and Link to the other Component:然后,您需要从 ButtonPage 组件中的react-router-dom
导入Link
并链接到另一个组件:
import { Link } from "react-router-dom";
export default function ButtonPage () {
return (
<div id = "signup">
<Link to="/getstarted">
<button className="signupbutton">Sign up</button>
</Link>
</div>
);
}
Et voilà: You linked two pages in React.等等:你在 React 中链接了两个页面。 For more information, look up the documentation of React-Router here .有关更多信息,请在此处查看React-Router 的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.