在一个REACT函数组件中实现事件功能。

huangapple go评论75阅读模式
英文:

Implementing events in a REACT function component

问题

我正在尝试弄清楚如何实现我的自定义事件。我在这里提出了问题,但是"event"这个词似乎让我的问题变得混淆了。有人建议我添加一个新的问题,所以我会尽量以另一种方式来解释:

相关帖子

我的组件:

import React, { useState } from "react";

const DropdownPaging2 = props => {
  function myClickFunc(val) {
    alert("我应该在这里添加什么来触发我的事件给消费组件");
  }

  return <div onClick={() => myClickFunc(100)}>点击我</div>;
};

export default DropdownPaging2;

在另一个组件(消费组件)的渲染函数中使用我的组件:

<DropdownPaging2></DropdownPaging2>

我想要实现的是,我可以将一个新的事件传递给消费组件。类似这样:

<DropdownPaging2 myCustomEvent={() => myCustomEvent(100)}></DropdownPaging2>
英文:

I am trying to figure out how to implement my own custom events. I asked the question here but the word event seems to confuse my question. I was asked to add a new question, so I will try to do my best in another way:

Related post

My component:

import React, { useState } from &quot;react&quot;;

const DropdownPaging2 = props =&gt; {
  function myClickFunc(val) {
    alert(&quot;WHAT SHOULD I ADD HERE TO FIRE MY EVENT TO THE CONSUMING COMPONENT&quot;);
  }

  return &lt;div onClick={() =&gt; myClickFunc(100)}&gt;CLICK me&lt;/div&gt;;
};

export default DropdownPaging2;

Using my component in another components (comsuming component) render function:

&lt;DropdownPaging2&gt;&lt;/DropdownPaging2&gt;

I would like implement so I can pass a new event to the consuming component. Something lige this:

&lt;DropdownPaging2 myCustomEvent={() =&gt; myCustomEvent(100)}&gt;&lt;/DropdownPaging2&gt;

答案1

得分: 0

只需在组件中使用custom回调(如果它作为prop传递),否则使用default回调。

return <div onClick={prop.myCustomEvent ? prop.myCustomEvent : () => myClickFunc(100)}>点击我</div>;
英文:

Just make your component use the custom callback if it was passed as a prob, otherwise use the default one.

return &lt;div onClick={prop.myCustomEvent ? prop.myCustomEvent : () =&gt; myClickFunc(100)}&gt;CLICK me&lt;/div&gt;;

答案2

得分: 0

你可以像你提到的那样将函数作为属性传递给你的DropdownPaging2组件:

<DropdownPaging2 myEvent={() => myCustomEvent(100)}></DropdownPaging2>

然后在组件中像这样使用它。

import React, { useState } from "react";

const DropdownPaging2 = props => {
  const myClickFunc = (val) => {
    if(props.myEvent){
      props.myEvent();
    } else {
      // 如果未传递函数,则使用默认值
    }
  }

  return <div onClick={() => myClickFunc(100)}>点击我</div>;
};

export default DropdownPaging2;
英文:

You can pass functions as props to your DropdownPaging2 component like you mentioned:

&lt;DropdownPaging2 myEvent={() =&gt; myCustomEvent(100)}&gt;&lt;/DropdownPaging2&gt;

And then use it in the component like this.


const DropdownPaging2 = props =&gt; {
  const myClickFunc = (val) =&gt; {
    if(props.myEvent){
      props.myEvent();
    } else {
      // default if no function is passed
     }

  }

  return &lt;div onClick={() =&gt; myClickFunc(100)}&gt;CLICK me&lt;/div&gt;;
};

export default DropdownPaging2; 

This way you are free to pass a custom function

huangapple
  • 本文由 发表于 2020年1月6日 21:14:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612791.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定