[英]Any way I can declare the event object rather than in the params of a function?
無論如何我可以將e object 放在handleSubmit function 內嗎? 我那里已經有一個參數,如果我將 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)}>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.