简体   繁体   English

如何将 function 作为 prop 传递给 React 中的组件

[英]How to pass function as prop to component in React

I have a component that generates input in Form.我有一个在表单中生成输入的组件。 I'm trying to pass a function to OnChange event, but always getting error我正在尝试将 function 传递给 OnChange 事件,但总是出错

import {  FormGroup, FloatingLabel, FormControl } from "react-bootstrap";

const FormInput = (props) =>{ 

return(

    <FormGroup controlId={props.controlId} className="mb-3"> 
    <FloatingLabel label={props.label}>
        <FormControl type={props.type} name={props.controlId} placeholder={props.label} onChange={props.onChange} />
    </FloatingLabel>
</FormGroup>
)

}

export default FormInput;

And in my App I'm passing props to Component.在我的应用程序中,我将道具传递给组件。 Can't figure out how to pass function to get value on input无法弄清楚如何通过 function 来获取输入值

import React, { Component } from "react";
import {
Container,
Row,
Form,
Button,

} from "react-bootstrap";
import FormInput from "./FormInput";
import {  FormGroup, FloatingLabel, FormControl } from "react-bootstrap";


class AddRecipe extends Component {
constructor() {
super();
this.state = {
  recipeImage: "",
  recipeName: "",
  recipeIngredients: "",
};


this.handleChange = this.handleChange.bind(this);

}
handleChange = (e) =>{
this.setState({
  [e.target.name]: e.target.value,
});

}

render() {
return (
  <Container>
    <Row>

    
      <Form inline="true" className="mt-5">
        <FormInput
          controlId="recipeImage"
          label="Image URL"
          type="url"
          handleChange={this.handleChange}
        />
        <FormInput
          controlId="recipeName"
          label="Recipe Name"
          type="text"
          handleChange={this.handleChange}
        />
        <FormInput
          controlId="recipeIngredients"
          label="Ingredients"
          type="textarea"
          handleChange={this.handleChange}
          
        />

        <Button>Add</Button>
      </Form>
    </Row>
  </Container>
);
}
}

export default AddRecipe;

How to pass handleChange function as props, so I can write value to my state?如何将 handleChange function 作为道具传递,以便我可以将值写入我的 state?

The problem is that in your component, you define the prop as onChange , but in your app, you pass the prop handleChange .问题在于,在您的组件中,您将道具定义为onChange ,但在您的应用程序中,您传递了道具handleChange To fix the error, just change the name of the prop you pass in your app:要修复错误,只需更改您在应用中传递的道具的名称:

<FormInput
  controlId="recipeImage"
  label="Image URL"
  type="url" 
  onChange={this.handleChange} 
/>
{/* this `onChange` prop should fix it */}

My mistake, I was using a different name for props.我的错误,我为道具使用了不同的名称。

Ater changing更换后

 <FormInput
      controlId="recipeIngredients"
      label="Ingredients"
      type="textarea"
      handleChange={this.handleChange}
      
    />

to

 <FormInput
          controlId="recipeIngredients"
          label="Ingredients"
          type="textarea"
          onChange={this.handleChange}
          
        />

It fixed issue.它解决了问题。 Thank you谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM