I want to pass my input values from form to handleSubmit () currently I am passing e.target.value
and getting an error cannot property 'value' of undefined.
following is the form code block from where I want to get the values
<Input
label="Write a message..."
name="message"
type="text"
/>
and the following is the code block from where I am trying to access the value under
handleSubmit = (e) => {
this.props.sendNewMessage(e.target.value);
}
Full code for reference :
import React from 'react';
import SubMenu from './SubMenu';
import MessageForm from './form/MessageForm';
import { sendNewMessage } from '../../actions/messages.actions'
import {connect} from 'react-redux';
class Messages extends React.PureComponent {
handleSubmit = (e) => {
this.props.sendNewMessage(e.target.value);
}
render() {
return (
<section className="page-notifications">
<SubMenu/>
<MessageForm onSubmit={this.handleSubmit}/>
</section>
)
}
}
const mapDispatchToProps = dispatch => {
return {
sendNewMessage: (msg) => dispatch(sendNewMessage(msg)),
}
}
export default connect(null,mapDispatchToProps)(Messages)
This isn't how you should handle form submissions. Your messageForm
should update your state using an onChange
handler. Then handleSubmit
should preventDefault()
and dispatch your sendNewMessage
action using the value(s) from state that have already been set.
The React docs are very helpful on this.
so I just need to pass "value" to handle submit ... well it worked