react router link with material ui button submit











up vote
0
down vote

favorite












I am using flask on the backend and react, react router and material-ui as a frontend.



I have:




  • material-ui/core 3.4.0,

  • react router dom 4.3.1


I am trying to create simple sign up form.
I would like to submit the form's data and then redirect to the index page using react router.
My problem is that material-ui button does not work with the link from router.
Is it possible to combine this two together somehow?



My code:



import:



import React, { Component } from 'react'
import { Link } from 'react-router-dom'
import { withStyles, Grid, Paper, Avatar, Typography, Button, TextField} from '@material-ui/core'
import LockIcon from '@material-ui/icons/LockOutlined'


and render return:



render() {
const { classes } = this.props
const { user: { username, password } } = this.state
return (
<Grid container className={classes.root}>
<Paper className={classes.paper}>

<Avatar className={classes.avatar}>
<LockIcon />
</Avatar>

<Typography component="h1" variant="h5">
Sign up
</Typography>

<form onSubmit={this.saveUser}>
<TextField label="Username" value={username} onChange={this.handleChange('username')} margin="normal" required fullWidth/>
<TextField label="Password" value={password} onChange={this.handleChange('password')} margin="normal" required fullWidth type='password'/>

<Button component={Link} to="/" type="submit" fullWidth variant="contained" color="primary">Sign Up</Button>

</form>
</Paper>
</Grid>
);
}


When I remove from Button component={Link} to="/":



<Button type="submit" fullWidth variant="contained" color="primary">Sign Up</Button>


then submit work and saves data in database. When I remove type="submit":



<Button component={Link} to="/" fullWidth variant="contained" color="primary">Sign Up</Button>


then redirect to index page.



Can I combine somehow this two in one button?



RaisedButton, FlatButton from material-ui not working anymore










share|improve this question









New contributor




Maniek Solek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    I am using flask on the backend and react, react router and material-ui as a frontend.



    I have:




    • material-ui/core 3.4.0,

    • react router dom 4.3.1


    I am trying to create simple sign up form.
    I would like to submit the form's data and then redirect to the index page using react router.
    My problem is that material-ui button does not work with the link from router.
    Is it possible to combine this two together somehow?



    My code:



    import:



    import React, { Component } from 'react'
    import { Link } from 'react-router-dom'
    import { withStyles, Grid, Paper, Avatar, Typography, Button, TextField} from '@material-ui/core'
    import LockIcon from '@material-ui/icons/LockOutlined'


    and render return:



    render() {
    const { classes } = this.props
    const { user: { username, password } } = this.state
    return (
    <Grid container className={classes.root}>
    <Paper className={classes.paper}>

    <Avatar className={classes.avatar}>
    <LockIcon />
    </Avatar>

    <Typography component="h1" variant="h5">
    Sign up
    </Typography>

    <form onSubmit={this.saveUser}>
    <TextField label="Username" value={username} onChange={this.handleChange('username')} margin="normal" required fullWidth/>
    <TextField label="Password" value={password} onChange={this.handleChange('password')} margin="normal" required fullWidth type='password'/>

    <Button component={Link} to="/" type="submit" fullWidth variant="contained" color="primary">Sign Up</Button>

    </form>
    </Paper>
    </Grid>
    );
    }


    When I remove from Button component={Link} to="/":



    <Button type="submit" fullWidth variant="contained" color="primary">Sign Up</Button>


    then submit work and saves data in database. When I remove type="submit":



    <Button component={Link} to="/" fullWidth variant="contained" color="primary">Sign Up</Button>


    then redirect to index page.



    Can I combine somehow this two in one button?



    RaisedButton, FlatButton from material-ui not working anymore










    share|improve this question









    New contributor




    Maniek Solek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am using flask on the backend and react, react router and material-ui as a frontend.



      I have:




      • material-ui/core 3.4.0,

      • react router dom 4.3.1


      I am trying to create simple sign up form.
      I would like to submit the form's data and then redirect to the index page using react router.
      My problem is that material-ui button does not work with the link from router.
      Is it possible to combine this two together somehow?



      My code:



      import:



      import React, { Component } from 'react'
      import { Link } from 'react-router-dom'
      import { withStyles, Grid, Paper, Avatar, Typography, Button, TextField} from '@material-ui/core'
      import LockIcon from '@material-ui/icons/LockOutlined'


      and render return:



      render() {
      const { classes } = this.props
      const { user: { username, password } } = this.state
      return (
      <Grid container className={classes.root}>
      <Paper className={classes.paper}>

      <Avatar className={classes.avatar}>
      <LockIcon />
      </Avatar>

      <Typography component="h1" variant="h5">
      Sign up
      </Typography>

      <form onSubmit={this.saveUser}>
      <TextField label="Username" value={username} onChange={this.handleChange('username')} margin="normal" required fullWidth/>
      <TextField label="Password" value={password} onChange={this.handleChange('password')} margin="normal" required fullWidth type='password'/>

      <Button component={Link} to="/" type="submit" fullWidth variant="contained" color="primary">Sign Up</Button>

      </form>
      </Paper>
      </Grid>
      );
      }


      When I remove from Button component={Link} to="/":



      <Button type="submit" fullWidth variant="contained" color="primary">Sign Up</Button>


      then submit work and saves data in database. When I remove type="submit":



      <Button component={Link} to="/" fullWidth variant="contained" color="primary">Sign Up</Button>


      then redirect to index page.



      Can I combine somehow this two in one button?



      RaisedButton, FlatButton from material-ui not working anymore










      share|improve this question









      New contributor




      Maniek Solek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I am using flask on the backend and react, react router and material-ui as a frontend.



      I have:




      • material-ui/core 3.4.0,

      • react router dom 4.3.1


      I am trying to create simple sign up form.
      I would like to submit the form's data and then redirect to the index page using react router.
      My problem is that material-ui button does not work with the link from router.
      Is it possible to combine this two together somehow?



      My code:



      import:



      import React, { Component } from 'react'
      import { Link } from 'react-router-dom'
      import { withStyles, Grid, Paper, Avatar, Typography, Button, TextField} from '@material-ui/core'
      import LockIcon from '@material-ui/icons/LockOutlined'


      and render return:



      render() {
      const { classes } = this.props
      const { user: { username, password } } = this.state
      return (
      <Grid container className={classes.root}>
      <Paper className={classes.paper}>

      <Avatar className={classes.avatar}>
      <LockIcon />
      </Avatar>

      <Typography component="h1" variant="h5">
      Sign up
      </Typography>

      <form onSubmit={this.saveUser}>
      <TextField label="Username" value={username} onChange={this.handleChange('username')} margin="normal" required fullWidth/>
      <TextField label="Password" value={password} onChange={this.handleChange('password')} margin="normal" required fullWidth type='password'/>

      <Button component={Link} to="/" type="submit" fullWidth variant="contained" color="primary">Sign Up</Button>

      </form>
      </Paper>
      </Grid>
      );
      }


      When I remove from Button component={Link} to="/":



      <Button type="submit" fullWidth variant="contained" color="primary">Sign Up</Button>


      then submit work and saves data in database. When I remove type="submit":



      <Button component={Link} to="/" fullWidth variant="contained" color="primary">Sign Up</Button>


      then redirect to index page.



      Can I combine somehow this two in one button?



      RaisedButton, FlatButton from material-ui not working anymore







      reactjs react-router material-ui






      share|improve this question









      New contributor




      Maniek Solek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Maniek Solek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 16 hours ago









      Roysh

      1,10921221




      1,10921221






      New contributor




      Maniek Solek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 17 hours ago









      Maniek Solek

      31




      31




      New contributor




      Maniek Solek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Maniek Solek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Maniek Solek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Add to state:



          doRedirect: false


          In render():



          <Button type="submit" fullWidth variant="contained" color="primary">Sign Up</Button>


          In submit hanler:



          saveUser = () => {
          // **********
          // Your code to update database
          // **********

          this.setState({ doRedirect: true });
          }


          In render():



          { this.state.doRedirect && <Redirect to="/" /> }


          And of course:



          import { Redirect } from 'react-router-dom'





          share|improve this answer





















          • I'm not sure where put this line: { this.state.doRedirect && <Redirect to="/" /> } in my code? I'm still a little beginner, so I would be grateful if you could help me with this one too? I understand the rest of the parts.
            – Maniek Solek
            15 hours ago










          • eg: <Grid container className={classes.root}> { this.state.doRedirect && <Redirect to="/" /> } <Paper className={classes.paper}>
            – Boris Traljić
            15 hours ago












          • It works! Thank you for your help, best regards ;)
            – Maniek Solek
            15 hours ago










          • Can you accept my answer? Please ;)
            – Boris Traljić
            15 hours ago


















          up vote
          0
          down vote













          In your save user function just redirect after saving the user.
          you can get history from props



          saveUser = event => {
          event.preventDefault()
          //update database with user
          this.props.history.location.push('new-route')
          // or you can use window.location
          window.location.href = 'new-route'
          }





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });






            Maniek Solek is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265186%2freact-router-link-with-material-ui-button-submit%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            Add to state:



            doRedirect: false


            In render():



            <Button type="submit" fullWidth variant="contained" color="primary">Sign Up</Button>


            In submit hanler:



            saveUser = () => {
            // **********
            // Your code to update database
            // **********

            this.setState({ doRedirect: true });
            }


            In render():



            { this.state.doRedirect && <Redirect to="/" /> }


            And of course:



            import { Redirect } from 'react-router-dom'





            share|improve this answer





















            • I'm not sure where put this line: { this.state.doRedirect && <Redirect to="/" /> } in my code? I'm still a little beginner, so I would be grateful if you could help me with this one too? I understand the rest of the parts.
              – Maniek Solek
              15 hours ago










            • eg: <Grid container className={classes.root}> { this.state.doRedirect && <Redirect to="/" /> } <Paper className={classes.paper}>
              – Boris Traljić
              15 hours ago












            • It works! Thank you for your help, best regards ;)
              – Maniek Solek
              15 hours ago










            • Can you accept my answer? Please ;)
              – Boris Traljić
              15 hours ago















            up vote
            0
            down vote



            accepted










            Add to state:



            doRedirect: false


            In render():



            <Button type="submit" fullWidth variant="contained" color="primary">Sign Up</Button>


            In submit hanler:



            saveUser = () => {
            // **********
            // Your code to update database
            // **********

            this.setState({ doRedirect: true });
            }


            In render():



            { this.state.doRedirect && <Redirect to="/" /> }


            And of course:



            import { Redirect } from 'react-router-dom'





            share|improve this answer





















            • I'm not sure where put this line: { this.state.doRedirect && <Redirect to="/" /> } in my code? I'm still a little beginner, so I would be grateful if you could help me with this one too? I understand the rest of the parts.
              – Maniek Solek
              15 hours ago










            • eg: <Grid container className={classes.root}> { this.state.doRedirect && <Redirect to="/" /> } <Paper className={classes.paper}>
              – Boris Traljić
              15 hours ago












            • It works! Thank you for your help, best regards ;)
              – Maniek Solek
              15 hours ago










            • Can you accept my answer? Please ;)
              – Boris Traljić
              15 hours ago













            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            Add to state:



            doRedirect: false


            In render():



            <Button type="submit" fullWidth variant="contained" color="primary">Sign Up</Button>


            In submit hanler:



            saveUser = () => {
            // **********
            // Your code to update database
            // **********

            this.setState({ doRedirect: true });
            }


            In render():



            { this.state.doRedirect && <Redirect to="/" /> }


            And of course:



            import { Redirect } from 'react-router-dom'





            share|improve this answer












            Add to state:



            doRedirect: false


            In render():



            <Button type="submit" fullWidth variant="contained" color="primary">Sign Up</Button>


            In submit hanler:



            saveUser = () => {
            // **********
            // Your code to update database
            // **********

            this.setState({ doRedirect: true });
            }


            In render():



            { this.state.doRedirect && <Redirect to="/" /> }


            And of course:



            import { Redirect } from 'react-router-dom'






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 16 hours ago









            Boris Traljić

            1126




            1126












            • I'm not sure where put this line: { this.state.doRedirect && <Redirect to="/" /> } in my code? I'm still a little beginner, so I would be grateful if you could help me with this one too? I understand the rest of the parts.
              – Maniek Solek
              15 hours ago










            • eg: <Grid container className={classes.root}> { this.state.doRedirect && <Redirect to="/" /> } <Paper className={classes.paper}>
              – Boris Traljić
              15 hours ago












            • It works! Thank you for your help, best regards ;)
              – Maniek Solek
              15 hours ago










            • Can you accept my answer? Please ;)
              – Boris Traljić
              15 hours ago


















            • I'm not sure where put this line: { this.state.doRedirect && <Redirect to="/" /> } in my code? I'm still a little beginner, so I would be grateful if you could help me with this one too? I understand the rest of the parts.
              – Maniek Solek
              15 hours ago










            • eg: <Grid container className={classes.root}> { this.state.doRedirect && <Redirect to="/" /> } <Paper className={classes.paper}>
              – Boris Traljić
              15 hours ago












            • It works! Thank you for your help, best regards ;)
              – Maniek Solek
              15 hours ago










            • Can you accept my answer? Please ;)
              – Boris Traljić
              15 hours ago
















            I'm not sure where put this line: { this.state.doRedirect && <Redirect to="/" /> } in my code? I'm still a little beginner, so I would be grateful if you could help me with this one too? I understand the rest of the parts.
            – Maniek Solek
            15 hours ago




            I'm not sure where put this line: { this.state.doRedirect && <Redirect to="/" /> } in my code? I'm still a little beginner, so I would be grateful if you could help me with this one too? I understand the rest of the parts.
            – Maniek Solek
            15 hours ago












            eg: <Grid container className={classes.root}> { this.state.doRedirect && <Redirect to="/" /> } <Paper className={classes.paper}>
            – Boris Traljić
            15 hours ago






            eg: <Grid container className={classes.root}> { this.state.doRedirect && <Redirect to="/" /> } <Paper className={classes.paper}>
            – Boris Traljić
            15 hours ago














            It works! Thank you for your help, best regards ;)
            – Maniek Solek
            15 hours ago




            It works! Thank you for your help, best regards ;)
            – Maniek Solek
            15 hours ago












            Can you accept my answer? Please ;)
            – Boris Traljić
            15 hours ago




            Can you accept my answer? Please ;)
            – Boris Traljić
            15 hours ago












            up vote
            0
            down vote













            In your save user function just redirect after saving the user.
            you can get history from props



            saveUser = event => {
            event.preventDefault()
            //update database with user
            this.props.history.location.push('new-route')
            // or you can use window.location
            window.location.href = 'new-route'
            }





            share|improve this answer

























              up vote
              0
              down vote













              In your save user function just redirect after saving the user.
              you can get history from props



              saveUser = event => {
              event.preventDefault()
              //update database with user
              this.props.history.location.push('new-route')
              // or you can use window.location
              window.location.href = 'new-route'
              }





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                In your save user function just redirect after saving the user.
                you can get history from props



                saveUser = event => {
                event.preventDefault()
                //update database with user
                this.props.history.location.push('new-route')
                // or you can use window.location
                window.location.href = 'new-route'
                }





                share|improve this answer












                In your save user function just redirect after saving the user.
                you can get history from props



                saveUser = event => {
                event.preventDefault()
                //update database with user
                this.props.history.location.push('new-route')
                // or you can use window.location
                window.location.href = 'new-route'
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 14 hours ago









                Gavyn Caldwell

                305




                305






















                    Maniek Solek is a new contributor. Be nice, and check out our Code of Conduct.










                     

                    draft saved


                    draft discarded


















                    Maniek Solek is a new contributor. Be nice, and check out our Code of Conduct.













                    Maniek Solek is a new contributor. Be nice, and check out our Code of Conduct.












                    Maniek Solek is a new contributor. Be nice, and check out our Code of Conduct.















                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265186%2freact-router-link-with-material-ui-button-submit%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    Popular posts from this blog

                    Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                    ComboBox Display Member on multiple fields

                    Is it possible to collect Nectar points via Trainline?