我很新ReactJS我只是想做一些小事情,以了解更多。ReactJS OnKeyPress触发按钮按
我想知道如果OnKeyPress可以触发按钮按下。我见过几个类似的问题,但触发的只是一个console.log或一个警报。所以我不知道如何触发按钮按下。
这是我到目前为止有:
class Form extends React.Component {
onButtonPress = (e) => {
// this is just an example of what happens when the button is pressed.
this.setState({isClicked: true});
}
keyPress = (event) => {
if (event.key == 'Enter'){
// How would I trigger the button that is in the render? I have this so far.
this.onButtonPress();
}
}
render() {
return (
<div>
<div className="fieldForm">
<input
value={name}
type="name"
onKeyPress={this.keyPress}
/>
</div>
<Button onClick={this.onButtonPress}>Submit</Button>
</div>
)
}
}
请注意,我并没有包括在这里的一切,如构造,道具,或者对象属性的状态。
这样做的目的是使它看起来像按钮已被点击。当按钮被点击时,它会在按钮上显示一个小的加载标志。如果我按下输入按钮(按钮上的加载标志,这就是为什么我要按下按钮),我想要发生同样的事情。
这可能吗?
相当肯定的是,'event.key'值是所有小写 – Derek