【问题标题】:How to pass an event parameter in addition to your own custom parameters in React when the custom parameter is getting data from child component当自定义参数从子组件获取数据时,如何在 React 中除了您自己的自定义参数之外传递事件参数
【发布时间】:2020-04-14 15:57:02
【问题描述】:

我正在使用 React 编程并在父 (TodoForm.js) 和子 (PrioritySelector.js) 组件之间传递数据。在这种情况下,我想从子组件获取数据,因此我将自定义参数传递给函数“handleSubmit”,然后将该函数作为 prop(getData={this.handleSubmit}) 传递给子组件。现在,我可以从子组件中检索数据,但是,当我想为 e.preventDefault 参数传递“e”参数时,它会给我一个错误。比如TypeError:e is not a function。有谁知道我可以在同一方法中传递这两个参数(自定义参数和事件参数)的方法?非常感谢。我的代码如下。谢谢!

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=( priorityLevelData, e)=> {
       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>
        )
    }
}



export default connect()(TodoForm);



    // PrioritySelector.js

import React from "react";
import $ from "jquery";
import { connect } from "react-redux";

class PrioritySelector extends React.Component  {


    componentDidMount() {
        $("#selector").show();
    }

    handleSelect =(e)=> {
       const priorityLevel = this.getPriorityLevel.value;
       this.props.getData(priorityLevel, e)
    }

    render() {
        console.log(this.props)
        return(
            <div>
                <div className="input-field col s12">
                    <select onChange={this.handleSelect} ref={(option)=> this.getPriorityLevel = option} id="selector">
                        <option disabled selected>Choose your option</option>
                        <option value="1">Low</option>
                        <option value="2">Medium</option>
                        <option value="3">High</option>
                    </select>
                </div>
            </div>
        )
    }

}


const mapStateToProps=(state)=> {
    return {
        priorityLevel: state
    }
}


export default connect(mapStateToProps)(PrioritySelector);

【问题讨论】:

    标签: javascript reactjs database react-native ecmascript-6


    【解决方案1】:

    您忘记将事件传递给getData

    onChange 将允许您访问该事件,但您仍然必须将其传递给您的 prop 函数。 它看起来像这样:

    handleSelect = e => {
      const priorityLevel = this.getPriorityLevel.value;
      this.props.getData(e, priorityLevel)
    }
    

    你的handleSubmit 函数也应该交换它们的值:

    handleSubmit=(e, priorityLevel)=> {
    

    你看我把它们调换了,让我解释一下原因。

    handleSubmit 将由添加按钮触发。这也将导致表单触发handleSubmit 函数。因此,该函数应该首先具有事件参数,然后您可以添加任何您想要的。

    您还应该将type="submit" 添加到您的按钮,这将触发表单操作。

    【讨论】:

    • 您好,谢谢您的回复。我已将事件添加到 handleselect 并对帖子进行了编辑。但是,现在它不会阻止默认操作。它正在刷新页面。
    • 请看我的改动
    • 我明白了。谢谢!该页面现在没有刷新,但是一旦我选择了priorityLevel,它就会提交表单。有新发现……哈哈……谢谢兄弟!
    【解决方案2】:

    在您的子组件 PrioritySelector 中,您有点击处理程序 handleSelect。 handleSelect 在单击时接收事件对象作为参数。您可以简单地将这个事件传递给父母 this.props.getData 道具

    handleSelect = (event) => {
      const priorityLevel = this.getPriorityLevel.value;
      this.props.getData(priorityLevel, event);
    }
    

    父组件的handleSubmit 现在可以访问从子组件传递过来的事件对象了。

    handleSubmit = (priorityLevelData, e) => {
      // now has the event object e
      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 = "";
    }
    

    【讨论】:

    • 感谢您的回复!你好,我试过这种方法。现在它没有给我一个错误,但它并没有阻止默认操作。当用户选择优先级时,它会刷新页面。
    猜你喜欢
    • 1970-01-01
    • 2010-10-24
    • 2015-09-26
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 2017-02-24
    相关资源
    最近更新 更多