Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 针对命中状态的合成事件?_Reactjs_Accessibility - Fatal编程技术网

Reactjs 针对命中状态的合成事件?

Reactjs 针对命中状态的合成事件?,reactjs,accessibility,Reactjs,Accessibility,我最近做了类似的事情 onHit(functionCall) { let attr = {}; attr["onKeyPress"] = functionCall; attr["onClick"] = functionCall; attr["onTouchEnd"] = functionCall; return attr; } 所以我可以在我的JSX中这样做 <a {...this.onHit((e)=>this.doSomething())

我最近做了类似的事情

onHit(functionCall)
{
    let attr = {};
    attr["onKeyPress"] = functionCall;
    attr["onClick"] = functionCall;
    attr["onTouchEnd"] = functionCall;
    return attr;
}
所以我可以在我的JSX中这样做

<a {...this.onHit((e)=>this.doSomething())} title="This has no href" tabIndex="0">This has no href</a>

一切都按预期进行,按键、点击和触地都会触发相同的事件

我创建了这个
onHit
函数,因为我正在构建一个web应用程序,其中所有动作控件都需要通过键盘、鼠标和触摸屏进行访问


在我继续使用自定义的
onHit
函数之前,在ReactJS中是否有更惯用的方法来实现这一点?

我认为您的解决方案很好,但您也可以创建一个自定义组件,将组件/元素类型作为prop,将
onHit
函数作为prop,并将其用作所有事件的事件处理程序,这就把剩下的道具分散开来了

示例

函数MyComponent({element:element,onHit,…rest}){
返回(
);
}
类应用程序扩展了React.Component{
render(){
返回(
console.log(“hit!”)}
title=“这没有href”
tabIndex=“0”
>
这没有href
);
}
}
render(,document.getElementById(“根”))