react router link with material ui button submit












0















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





























    0















    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



























      0












      0








      0


      0






      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
















      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















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 15:49









      Roysh

      1,12321221




      1,12321221










      asked Nov 12 '18 at 15:22









      Maniek SolekManiek Solek

      31




      31
























          2 Answers
          2






          active

          oldest

          votes


















          0














          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
            Nov 12 '18 at 16:52











          • eg: <Grid container className={classes.root}> { this.state.doRedirect && <Redirect to="/" /> } <Paper className={classes.paper}>

            – Boris Traljić
            Nov 12 '18 at 17:01













          • It works! Thank you for your help, best regards ;)

            – Maniek Solek
            Nov 12 '18 at 17:10











          • Can you accept my answer? Please ;)

            – Boris Traljić
            Nov 12 '18 at 17:34



















          0














          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',
            autoActivateHeartbeat: false,
            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
            });


            }
            });














            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















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            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
              Nov 12 '18 at 16:52











            • eg: <Grid container className={classes.root}> { this.state.doRedirect && <Redirect to="/" /> } <Paper className={classes.paper}>

              – Boris Traljić
              Nov 12 '18 at 17:01













            • It works! Thank you for your help, best regards ;)

              – Maniek Solek
              Nov 12 '18 at 17:10











            • Can you accept my answer? Please ;)

              – Boris Traljić
              Nov 12 '18 at 17:34
















            0














            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
              Nov 12 '18 at 16:52











            • eg: <Grid container className={classes.root}> { this.state.doRedirect && <Redirect to="/" /> } <Paper className={classes.paper}>

              – Boris Traljić
              Nov 12 '18 at 17:01













            • It works! Thank you for your help, best regards ;)

              – Maniek Solek
              Nov 12 '18 at 17:10











            • Can you accept my answer? Please ;)

              – Boris Traljić
              Nov 12 '18 at 17:34














            0












            0








            0







            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 Nov 12 '18 at 16:08









            Boris TraljićBoris Traljić

            494311




            494311













            • 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
              Nov 12 '18 at 16:52











            • eg: <Grid container className={classes.root}> { this.state.doRedirect && <Redirect to="/" /> } <Paper className={classes.paper}>

              – Boris Traljić
              Nov 12 '18 at 17:01













            • It works! Thank you for your help, best regards ;)

              – Maniek Solek
              Nov 12 '18 at 17:10











            • Can you accept my answer? Please ;)

              – Boris Traljić
              Nov 12 '18 at 17:34



















            • 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
              Nov 12 '18 at 16:52











            • eg: <Grid container className={classes.root}> { this.state.doRedirect && <Redirect to="/" /> } <Paper className={classes.paper}>

              – Boris Traljić
              Nov 12 '18 at 17:01













            • It works! Thank you for your help, best regards ;)

              – Maniek Solek
              Nov 12 '18 at 17:10











            • Can you accept my answer? Please ;)

              – Boris Traljić
              Nov 12 '18 at 17:34

















            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
            Nov 12 '18 at 16:52





            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
            Nov 12 '18 at 16:52













            eg: <Grid container className={classes.root}> { this.state.doRedirect && <Redirect to="/" /> } <Paper className={classes.paper}>

            – Boris Traljić
            Nov 12 '18 at 17:01







            eg: <Grid container className={classes.root}> { this.state.doRedirect && <Redirect to="/" /> } <Paper className={classes.paper}>

            – Boris Traljić
            Nov 12 '18 at 17:01















            It works! Thank you for your help, best regards ;)

            – Maniek Solek
            Nov 12 '18 at 17:10





            It works! Thank you for your help, best regards ;)

            – Maniek Solek
            Nov 12 '18 at 17:10













            Can you accept my answer? Please ;)

            – Boris Traljić
            Nov 12 '18 at 17:34





            Can you accept my answer? Please ;)

            – Boris Traljić
            Nov 12 '18 at 17:34













            0














            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




























              0














              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


























                0












                0








                0







                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 Nov 12 '18 at 18:16









                Gavyn CaldwellGavyn Caldwell

                326




                326






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    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















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Full-time equivalent

                    さくらももこ

                    13 indicted, 8 arrested in Calif. drug cartel investigation