【问题标题】:Onsubmit and history push is not working react routerOnsubmit 和历史推送不起作用反应路由器
【发布时间】:2018-12-05 05:39:22
【问题描述】:

我正在使用材料 ui,但是当我点击提交时,它不会重定向。我正在使用 withRouter,但接下来会发生什么,

   http://localhost:3000/SignIn

它重定向到

  http://localhost:3000/SignIn?email=abc%40yahoo.in&password=dddddddddddddd

我有 src/components/userDashboard,当点击登录时它必须重定向。如果成功检查路由是否有效,则只是重定向。

1)我在这里不明白的一件事是,如果用户没有任何历史记录 n 第一次来,那么这个历史记录会起作用吗?或者我做错了什么?我是新手路由我不知道我应该做什么?

     this.props.history.push('/Userdashboard');

src/SingIn.js

                import React from 'react';
            import PropTypes from 'prop-types';
            import Avatar from '@material-ui/core/Avatar';
            import Button from '@material-ui/core/Button';
            import CssBaseline from '@material-ui/core/CssBaseline';
            import FormControl from '@material-ui/core/FormControl';
            import FormControlLabel from '@material-ui/core/FormControlLabel';
            import Checkbox from '@material-ui/core/Checkbox';
            import Input from '@material-ui/core/Input';
            import InputLabel from '@material-ui/core/InputLabel';
            import LockIcon from '@material-ui/icons/LockOutlined';
            import Paper from '@material-ui/core/Paper';
            import Typography from '@material-ui/core/Typography';
            import withStyles from '@material-ui/core/styles/withStyles';
            import { withRouter } from 'react-router';

            const styles = theme => ({
            main: {
                width: 'auto',
                display: 'block', // Fix IE 11 issue.
                marginLeft: theme.spacing.unit * 3,
                marginRight: theme.spacing.unit * 3,
                [theme.breakpoints.up(400 + theme.spacing.unit * 3 * 2)]: {
                width: 400,
                marginLeft: 'auto',
                marginRight: 'auto',
                },
            },
            paper: {
                marginTop: theme.spacing.unit * 8,
                display: 'flex',
                flexDirection: 'column',
                alignItems: 'center',
                padding: `${theme.spacing.unit * 2}px ${theme.spacing.unit * 3}px ${theme.spacing.unit * 3}px`,
            },
            avatar: {
                margin: theme.spacing.unit,
                backgroundColor: theme.palette.secondary.main,
            },
            form: {
                width: '100%', // Fix IE 11 issue.
                marginTop: theme.spacing.unit,
            },
            submit: {
                marginTop: theme.spacing.unit * 3,
            },
            });

            class SignIn extends React.Component {

                handleSubmit(){
                      //as nothing but a redirect if successfull to check whether routes work or not
                    this.props.history.push('/Userdashboard');
                }

                render(){
                    const { classes } =this.props;
                // handleSubmit();
                    return (
                        <main className={classes.main}>
                        <CssBaseline />
                        <Paper className={classes.paper}>
                            <Avatar className={classes.avatar}>
                            <LockIcon />
                            </Avatar>
                            <Typography component="h1" variant="h5">
                            Sign in
                            </Typography>
                            <form className={classes.form} onSubmit={this.handleSubmit}>
                            <FormControl margin="normal" required fullWidth>
                                <InputLabel htmlFor="email">Email Address</InputLabel>
                                <Input id="email" name="email" autoComplete="email" autoFocus />
                            </FormControl>
                            <FormControl margin="normal" required fullWidth>
                                <InputLabel htmlFor="password">Password</InputLabel>
                                <Input name="password" type="password" id="password" autoComplete="current-password" />
                            </FormControl>
                            <FormControlLabel
                                control={<Checkbox value="remember" color="primary" />}
                                label="Remember me"
                            />
                            <Button
                                type="submit"
                                fullWidth
                                variant="contained"
                                color="primary"
                                className={classes.submit}
                            >
                                Sign in
                            </Button>
                            </form>
                        </Paper>
                        </main>
                    );
                }
            }

            SignIn.propTypes = {
            classes: PropTypes.object.isRequired,
            };

            export default  withRouter(withStyles(styles)(SignIn));

2)为什么on submit 没有路由到路由? 好难理解?

我收到以下错误

                TypeError: Cannot read property 'props' of undefined
                handleSubmit
                E:/reacr-redux/src/components/SignIn.js:53
                50 | 
                51 | handleSubmit(event){
                52 |     event.preventDefault();
                > 53 |      this.props.history.push('/Userdashboard');
                    | ^  54 | }
                55 | 
                56 | render(){

【问题讨论】:

    标签: reactjs react-router


    【解决方案1】:

    如果你使用的是 babel 的插件 stage-1,你应该为那个函数使用这种格式

    handleSubmit = (event) => {
      event.preventDefault();
      this.props.history.push('/Userdashboard');
    }
    

    否则,您可能希望在类的顶部使用构造函数绑定,例如:

      constructor(props) {
        super(props);
        this.handleSubmit = this.handleSubmit.bind(this);
      }
    

    read this article for details.

    【讨论】:

    • 我做了 onSubmit={this.handleSubmit.bind(this)} 我的坏事忘记了。你能告诉我在我的项目中哪里可以找到这个“babel's plugin stage-1”吗?在 package.json 中找到它
    • 这将是一个完整的参考,medium.com/@joshblack/…
    【解决方案2】:

    试试这个:

    handleSubmit = (event) => {
                    event.preventDefault();
                    this.props.history.push('/Userdashboard');
                }
    

    这个 history.push 将首次工作。

    【讨论】:

    • 不起作用。抛出错误。我已经更新了我已经更新了我的问题。 TypeError:无法读取未定义的属性“道具”
    猜你喜欢
    • 2021-04-03
    • 2020-09-06
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 2018-01-04
    • 2016-05-28
    相关资源
    最近更新 更多