简体   繁体   English

手动触发 onChange 事件

[英]Trigger onChange event manually

I would like to call my onChange manually.我想手动调用我的 onChange。

Here's my select with onChange (I pass onChangeSelect as a prop from parent component)这是我的 onChange 选择(我将 onChangeSelect 作为来自父组件的道具传递)

return(
   <Form Control disabled={disabled}
      <NativeSelect ref={ref} onChange={onChangeSelect}>
       ...

And I'd like to call that onChange every time my variable changes and is empty我想在每次我的变量改变并且为空时调用它

useEffect(() => {
    if (requiredSelect[0].length > 0 || id === "root1") { setDisabled(false) }
    else { ref.current.value = ([[], []]); ref.current.onChange ; setDisabled(true); }
}, [requiredSelect])

And here's onChangeSelect in parent component这是父组件中的 onChangeSelect

<Child onChangeSelect={(e) => rootChange(e)}>

and what it does以及它的作用

const rootChange = e => { setRootSelect(e.target.value.split(',')); }

The simplest solution here would be to change the definition of your rootChange function to accept the value instead of the event itself.这里最简单的解决方案是更改rootChange函数的定义以接受value而不是event本身。

const rootChange = value => { setRootSelect(value.split(',')); }

// In parent:
<Child onChangeSelect={rootChange}>

// Select
<NativeSelect ref={ref} onChange={(e) => onChangeSelect(e.target.value)}>

You can trigger the function manually with:您可以通过以下方式手动触发该功能:

onChangeSelect(whateverValueYouWant); // notice that you need the brackets when calling the function.

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

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