react redox form tag onSubmit does not work

Asked 2 years ago, Updated 2 years ago, 140 views

Currently learning react-redux

I wrote the following code, but the onSubmit in the form tag does not work.
OnChange in the input tag works fine

I would appreciate it if someone could let me know

import React, {Component} from 'react'
import {connect} from 'react-redux'
import {addPost} from '../actions/postAction'


US>class AddPost extensions Component {
    state = {
        title:',
        body: '
    }

    handleChangeTitle=(e)=>{
        This.setState({
            title —e.target.value
        })
        console.log(this.state)
    }

    handleChangeBody=(e)=>{
        This.setState({
            body —e.target.value
        })
        console.log(this.state)
    }

    handleSubmit=(e)=>{
        e.preventDefault();
        This.props.mapDispatchToProps (this.props.post.id)
        This.setState({
            title:',
            body: ' '
        })
    }

    render() {
        return(
            <div>
                <form onSubmit={this.handleSubmit}>
                    <label> title</label>
                    <input type="text" id="title" onChange={this.handleChangeTitle} value={this.state.title}/>
                    <label>body</label>
                    <input type="text" id="body" onChange={this.handleChangeBody}value={this.state.body}/>
                </form>
            </div>
        )
    }
}

const mapDispatchToProps=(dispatch)=>{
    US>return{
        addPost:(title, body)=>{dispatch(addPost(title, body))}
    }
}

export default connect (mapDispatchToProps) (AddPost); 

Please refer to this answer
I wrote the constructor, but I couldn't make it work

https://stackoverflow.com/questions/35098324/react-form-component-onsubmit-handler-not-working

javascript html reactjs redux

2022-09-30 21:39

2 Answers

Maybe it's because there's no element to ignite the submit event?

onSubmit in <form> is called when a submit event fires.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit_event

Add the <button> or <input type="submit"> tag in <form> and click the button to call onSubmit.

<form onSubmit={this.handleSubmit}>
    <label> title</label>
    <input type="text" id="title" onChange={this.handleChangeTitle} value={this.state.title}/>
    <label>body</label>
    <input type="text" id="body" onChange={this.handleChangeBody}value={this.state.body}/>
    <input type="submit" value="add"/>
</form>

Note: https://ja.reactjs.org/docs/forms.html


2022-09-30 21:39

<input type="text">You may expect to submit when you press the enter key, but in order to do such an implicit submit (implicit submission), it must be one of the following:

  • One send field
  • with submit button

There are two fields to send this time, so the submit button is required.

Note:

HTML Living Standard—Last Updated 22 October 2020>4.10.21.2 Implicit submission:

If the form has no submit button, then the implicit submission mechanism must do anything if the form has more than one field that blocks permit submission,


2022-09-30 21:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.