admin管理员组文章数量:1335406
I want to create a simple form that takes in an email adress and later adds it to our database. I went with React Forms, because it facilitates the whole development process and reduces the amount of time.
However, when I'm trying to POST my form I'm getting this error: Uncaught Error: You must either pass handleSubmit() an onSubmit function or pass onSubmit as a prop
Here's my AddUserForm.js:
import React from 'react'
import { Field, reduxForm } from 'redux-form'
const AddUserForm = ({ handleSubmit }) => {
return (
<form onSubmit={handleSubmit}>
<div>
<Field name="email" ponent="input" type="email" />
</div>
<button type="submit">Bjud in</button>
</form>
)
}
export default reduxForm({
form: 'addUser'
})(AddUserForm)
Here's my addUserAction:
import axios from 'axios'
import settings from '../settings'
axios.defaults.baseURL = settings.hostname
export const addUser = email => {
return dispatch => {
return axios.post('/invite', { email: email }).then(response => {
console.log(response)
})
}
}
And here's my AddUserContainer.js:
import React, { Component } from 'react'
import { addUser } from '../../actions/addUserAction'
import AddUserForm from './Views/AddUserForm'
import { connect } from 'react-redux'
class AddUserContainer extends Component {
submit(values) {
console.log(values)
this.props.addUser(values)
}
render() {
return (
<div>
<h1>Bjud in användare</h1>
<AddUserForm onSubmit={this.submit.bind(this)} />
</div>
)
}
}
function mapStateToProps(state) {
return { user: state.user }
}
export default connect(mapStateToProps, { addUser })(AddUserContainer)
Thanks for reading!
I want to create a simple form that takes in an email adress and later adds it to our database. I went with React Forms, because it facilitates the whole development process and reduces the amount of time.
However, when I'm trying to POST my form I'm getting this error: Uncaught Error: You must either pass handleSubmit() an onSubmit function or pass onSubmit as a prop
Here's my AddUserForm.js:
import React from 'react'
import { Field, reduxForm } from 'redux-form'
const AddUserForm = ({ handleSubmit }) => {
return (
<form onSubmit={handleSubmit}>
<div>
<Field name="email" ponent="input" type="email" />
</div>
<button type="submit">Bjud in</button>
</form>
)
}
export default reduxForm({
form: 'addUser'
})(AddUserForm)
Here's my addUserAction:
import axios from 'axios'
import settings from '../settings'
axios.defaults.baseURL = settings.hostname
export const addUser = email => {
return dispatch => {
return axios.post('/invite', { email: email }).then(response => {
console.log(response)
})
}
}
And here's my AddUserContainer.js:
import React, { Component } from 'react'
import { addUser } from '../../actions/addUserAction'
import AddUserForm from './Views/AddUserForm'
import { connect } from 'react-redux'
class AddUserContainer extends Component {
submit(values) {
console.log(values)
this.props.addUser(values)
}
render() {
return (
<div>
<h1>Bjud in användare</h1>
<AddUserForm onSubmit={this.submit.bind(this)} />
</div>
)
}
}
function mapStateToProps(state) {
return { user: state.user }
}
export default connect(mapStateToProps, { addUser })(AddUserContainer)
Thanks for reading!
Share Improve this question asked Jul 26, 2017 at 9:29 Martin NordströmMartin Nordström 6,02511 gold badges33 silver badges66 bronze badges 5-
I get:
onSubmit
is not defined. – Martin Nordström Commented Jul 26, 2017 at 9:33 -
export default connect()(reduxForm({ form: 'addUser', onSubmit, })(AddUserForm))
. Also remember to importconnect
from react-redux. – kind user Commented Jul 26, 2017 at 9:36 -
onSubmit
is still not defined – Martin Nordström Commented Jul 26, 2017 at 9:38 - Give me a sec, I will post a full answer. – kind user Commented Jul 26, 2017 at 9:39
- Appreciate that – Martin Nordström Commented Jul 26, 2017 at 9:50
1 Answer
Reset to default 4onSubmit
is not defined because it's not declared. Follow the path:
Note: values
variable holds fields data, in your case it will hold typed email.
import React from 'react';
import { Field, reduxForm } from 'redux-form';
import { connect } from 'react-redux';
const AddUserForm = ({ handleSubmit }) => {
return (
<form onSubmit={handleSubmit}>
<div>
<Field name="email" ponent="input" type="email" />
</div>
<button type="submit">Bjud in</button>
</form>
)
}
const onSubmit = (values, dispatch) => {
dispatch( // your submit action // );
};
export default connect()(reduxForm({
form: 'addUser',
onSubmit,
})(AddUserForm));
本文标签:
版权声明:本文标题:javascript - Redux Form - You must either pass handleSubmit() an onSubmit function or pass onSubmit as a prop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742373981a2462791.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论