简体   繁体   English

单击按钮时使用 React 导航到新页面

[英]Navigate to a new page with React when I click on a button

I want to navigate from welcome page to the login page, I created the 2 pages and now I want to navigate to the login page when I click on the Login button.我想从欢迎页面导航到登录页面,我创建了 2 个页面,现在我想在单击登录按钮时导航到登录页面。

Here is the welcome page code:这是欢迎页面代码:

import React from 'react';
import Button from '@material-ui/core/Button';
import Box from '@material-ui/core/Box'

const useStyles = makeStyles({
    logo: {
        width: '120px',
        height: '120px',
    },
    loginButton: {
        right: '50px',
    }
});

export default function Welcome() {
const classes = useStyles()
return (
        <div>
        <Box style={{ width: '100%' }} display="flex" justifyContent="center" alignItems="center" >

            <Box flexGrow={1}>
            <img src={require('../logo.svg')} className={classes.logo} />
            </Box>

            <Box>
            <Button variant="contained" color="primary" className={classes.loginButton}>
                Login
            </Button>
            </Box>

        </Box>
</div>

And here is the Login component ( not yet finished ):这是登录组件(尚未完成):

import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import TextField from '@material-ui/core/TextField';

const useStyles = makeStyles((theme) => ({
    root: {
      '& > *': {
        margin: theme.spacing(1),
        width: '25ch',
      },
    },
  }));

export default function Login() {
    const classes = useStyles();
    return (
        <div>
            <TextField id="email" label="Enter email" />
            <TextField id="password" label="Enter password" />
        </div>
    )
}

PS: I'm using react material ui library PS:我正在使用反应材料 ui 库

You are doing great, and also you have written your Welcome and Login , React functional components very well.你做得很好,你的WelcomeLoginReact functional components也写得很好。

To navigate programmatically to another page you have to use history object provided by React Router DOM .要以navigate programmatically到另一个页面,您必须使用由React Router DOM提供的history object 。 And create a function that is can be executed using any button onClick method.并创建一个 function 可以使用任何按钮onClick方法执行。

import { useHistory } from 'react-router-dom';


export default function Login() {

   const classes = useStyles();
   const loginHandler(){
       // handle your login logic
       history.push('/help'); // use either relative or absolute path both will work,navigate to help page or wherever want to navigate
   };

   return (
       <div>
            <TextField id="email" label="Enter email" />
            <TextField id="password" label="Enter password" />
            <button onClick={loginHandler}>Login</button>
       </div>
   );
}

You were really close with your comment reply:您的评论回复非常接近:

<Router> <Route path="/login" component={Login}> <Button variant="contained" color="primary" className={classes.loginButton}> Login </Button> </Route></Router>

The way that works for me is to make a router file with a constant that contains my routers.对我有用的方法是制作一个包含我的路由器的常量的路由器文件。 It would look like this:它看起来像这样:

Router.js路由器.js

import React from "react";
import { Route } from "react-router-dom";
import Login from " *wherever your login is* ";
import Welcome from " *wherever your welcome is* ";

const Router = () => (
  <div>
    <Route exact path="/" component={Welcome} />
    <Route exact path="/login" component={Login} />
  </div>
);

export default Router;

You would then make the only thing in your App.js file the router:然后,您将App.js文件中的唯一内容设置为路由器:

App.js应用程序.js

import Router from " *where you put your router file* ";

class App extends Component {
  render() {
    return (
      <div>
         <Router>
            <Router />
         </Router>
      </div>
    );
  }
}

export default App;

Finally, you would make your button a link to the designated url in your router:最后,您可以让您的按钮链接到路由器中指定的 url:

Welcome.js欢迎.js

import React from 'react';
import Button from '@material-ui/core/Button';
import Box from '@material-ui/core/Box';
import { Link } from "react-router-dom"; 
//^this import is important

...

<Button component={Link} to='/login' variant="contained" color="primary" className={classes.loginButton}>
Login
</Button>

That should set you off.那应该会让你失望。 I'm kind of new to this as well so tell me if something is broken.我对此也很陌生,所以请告诉我是否有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM