最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass two functions to onClick event in react - Stack Overflow

programmeradmin0浏览0评论

I want to pass two functions to onClick event which is handleSubmit and handleDelete to the HomePage.js from the HomeItem.js

Here is my Error: No duplicate props allowed react/jsx-no-duplicate-props.

Here is my HomePage.js:

 const HomePage = props => {
  const tvshow = props.item;
  let res;

  if (tvshow.length > 0) {
    res = tvshow.map(res=> (
      <Content item={res} onClick={props.onClick}/>
    ));
  }

  return (
    <div>
      <Container>
        <Row>{res}</Row>
      </Container>
    </div>
  );
};

export default HomePage;

Here is my HomeItem.js:

const HomeItem = props => {
  function handleSubmit() {
    props.onClick({
      name: props.item.name,
      id: props.item.id
    });
  }

  function handleName() {
    props.onClick({
      name: props.item.name
    });
  }

<Button onClick={handleSubmit}></Button>
<Button onClick={handleName}></Button>

Here is my App.js:

handleSubmit(newFavorite) {}
handleName(newFavorite) {}

render() {
  <Route
    exact
    path="/"
    render={() => (
      <HomePage
        item={this.state.SaveFavorite}
        onClick={this.handleSubmit}
        onClick={this.handleName}
      />
    )}
  />
} 

So my question is how to put 2 onClick function to the Hompage.js

I want to pass two functions to onClick event which is handleSubmit and handleDelete to the HomePage.js from the HomeItem.js

Here is my Error: No duplicate props allowed react/jsx-no-duplicate-props.

Here is my HomePage.js:

 const HomePage = props => {
  const tvshow = props.item;
  let res;

  if (tvshow.length > 0) {
    res = tvshow.map(res=> (
      <Content item={res} onClick={props.onClick}/>
    ));
  }

  return (
    <div>
      <Container>
        <Row>{res}</Row>
      </Container>
    </div>
  );
};

export default HomePage;

Here is my HomeItem.js:

const HomeItem = props => {
  function handleSubmit() {
    props.onClick({
      name: props.item.name,
      id: props.item.id
    });
  }

  function handleName() {
    props.onClick({
      name: props.item.name
    });
  }

<Button onClick={handleSubmit}></Button>
<Button onClick={handleName}></Button>

Here is my App.js:

handleSubmit(newFavorite) {}
handleName(newFavorite) {}

render() {
  <Route
    exact
    path="/"
    render={() => (
      <HomePage
        item={this.state.SaveFavorite}
        onClick={this.handleSubmit}
        onClick={this.handleName}
      />
    )}
  />
} 

So my question is how to put 2 onClick function to the Hompage.js

Share Improve this question edited Oct 31, 2018 at 16:07 brietsparks 5,0168 gold badges39 silver badges70 bronze badges asked Oct 31, 2018 at 13:24 CR97CR97 851 gold badge3 silver badges14 bronze badges 2
  • What exactly do you want to achieve? 2 different functionalities for the button? or when you click a button you want to run one function and then a second one? – c-chavez Commented Oct 31, 2018 at 13:57
  • why 2 functions for one onClick? why not, 2 separate ones? like onSubmitClick and onNameClick? – brietsparks Commented Oct 31, 2018 at 15:56
Add a ment  | 

3 Answers 3

Reset to default 5

How about this:

<HomePage
        item={this.state.SaveFavorite}
        onClick={(favorite)=>{
            this.handleSubmit(favorite);
            this.handleName(favorite);
            }
        }
        />

This assumes your goal is to call both functions one at a time. If they should be called in different situations give one function a different name, eg onSubmit or onNameChange.

Try This:

<HomePage
     item={this.state.SaveFavorite}
     onClick={(newFavorite) => this.handleSubmit(newFavorite);this.handleName(newFavorite)}
 />

you can pass multiple functions to events in react, let say changeEvent, to do follow those steps.

1- create your function two or the number of function you like.

2- create an object that contains those functions

3- pass the object as a props to where it would be consumed

4- choose the correspondant function to each form or whatever you need.

here is an example, this sample is with typescript.

  const onChangeFunctions = {
    onChangeForm1: handleChangeForm1,
    onChangeForm2: handleChangeForm2,
};

   <MainForm
      onChange={onChangeFunctions} // here is your function
      datas={yourData}
      otherProps={otherProps}
    />

Now you use the fucntion on the child ponents

interface PropsFrom {
  model1: Model1;
  model2: Model2;
  onChange: any;
}

export default function ProductForm(props: PropsForm) {
 return (
  <Container maxWidth="lg">
    <Grid item md={6}>
      <FormOne
        model={props.model1} // the model that bind your form
        onChange={props.onChange.onChangeForm1} // here you can use your first function
      />
    </Grid>

   <Grid item md={6}>
      <FormTwo
        model={props.model2} // the model that bind your form
        onChange={props.onChange.onChangeForm2} // here you can use your second function
        />
      </Grid>
    </Grid>
  </Container>

for javascript just pass the functions as props and delete the interface from the child ponents.

发布评论

评论列表(0)

  1. 暂无评论