react router link with material ui button submit
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
add a comment |
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
add a comment |
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
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
reactjs react-router material-ui
edited Nov 12 '18 at 15:49
Roysh
1,12321221
1,12321221
asked Nov 12 '18 at 15:22
Maniek SolekManiek Solek
31
31
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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'
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
add a comment |
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'
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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'
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
add a comment |
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'
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
add a comment |
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'
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'
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
add a comment |
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
add a comment |
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'
}
add a comment |
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'
}
add a comment |
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'
}
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'
}
answered Nov 12 '18 at 18:16
Gavyn CaldwellGavyn Caldwell
326
326
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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