简体   繁体   English

如何在 React js 中从一个 HTML 页面路由到另一个 HTML 页面?

[英]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导入BrowserRouterRouteRoutes

    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.

相关问题 如何将 state 从一条反应路线传递到另一条反应路线? - How can i pass a state from one react route to another react route? 如何将数据从一个页面传递到另一个 React JS - How can I pass data from one page to another React JS 在 React/Next.js 中,如何简单地将数据从一个页面的组件传递到另一个页面上的组件? - How can I simply pass data from a component one page to a component on another page in React/Next.js? 我如何从Angular页面路由到React页面? - How can i route to React page from an Angular page? 如何从react.js的一页转到另一个html文件 - how to go from one page of react.js to another html file 如何使用javascript将值从一个html页面传递到另一个html页面 - How can I pass values from one html page to another html page using javascript Javascript:我可以使用 JS 将 HTML 元素从一页复制到另一页吗? - Javascript: Can I Use JS to Copy an HTML Element From One Page to Another? 如何在 React JS 中从一个页面导航到另一个页面? - how to navigate from one page to another in react js? 如何在html中将该值从一个页面获取到另一个页面 - How can i get that value in html from one page to another page React - 如何将 API 数据从一个组件传递到另一个 js 文件中的另一个组件? - React - How can I pass API data from one component to another in a different js file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM