无论如何,我可以将e对象放在handleSubmit函数中吗?我已经有一个参数了,如果我将e添加到参数中,则根据参数的顺序会给我一个错误,如果handleSubmit =(e,priorityLevelData)=,则说“ TypeError:e.preventDefault不是函数” > {。 ....或“ TypeError:无法读取未定义的属性'preventDefault'”,如果handleSubmit =(priorityLevelData,e)=> {
import React from "react";
import PrioritySelector from "./PrioritySelector";
import { connect } from "react-redux";
class TodoForm extends React.Component {
/*submit handler to grab input from the input references and store them
in the "todoData" object. Then dispatches an action type and payload
capturing the data. Then clears the input*/
handleSubmit=(e, priorityLevelData)=> {
e.preventDefault()
const todoTitle = this.getTodoTitle.value;
const description = this.getDescription.value;
const priorityLevel = priorityLevelData;
const todoData = {
id: Math.floor(Math.random()*1000),
todoTitle,
description,
priorityLevel,
editing: false
}
this.props.dispatch({type:"ADD_TODO", todoData })
this.getTodoTitle.value = "";
this.getDescription.value = "";
}
render() {
console.log(this.props, "TODOFORMPROPS")
return(
<div>
<form onSubmit={this.handleSubmit}>
<input type="text" ref={(input)=> this.getTodoTitle=input} placeholder="Enter Todo" required/>
<input type="text" ref={(input)=> this.getDescription=input} placeholder="Enter Description" required/>
<PrioritySelector getData={this.handleSubmit} />
<button>Add Todo</button>
</form>
</div>
)
}
}
const mapStateToProps = (state) => {
return {
priorityLevel: state
}
}
export default connect(mapStateToProps)(TodoForm);
最佳答案
做这个:
<form onSubmit={e => this.handleSubmit(e)}>
您还可以将任何其他附加数据传递给
handleSubmit()
,例如this.state
:<form onSubmit={e => this.handleSubmit(e, this.state)}>