Warm tip: This article is reproduced from stackoverflow.com, please click
reactjs redux

I want to pass my input values from Form to handleSubmit ()

发布于 2020-03-27 10:23:34

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)
Questioner
RebelCoder
Viewed
96
Will Jenkins 2019-07-02 21:33

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.