gpt4 book ai didi

javascript - 如何在用户单击 Enter 按钮时停止触发按钮 React js?

转载 作者:太空宇宙 更新时间:2023-11-04 16:11:05 25 4
gpt4 key购买 nike

我是 React js 的新手,我对如何在用户单击 Enter 按钮时停止触发简单按钮感到困惑......抱歉,我的代码会很尴尬......如果阅读起来不舒服,请随时将其注释掉

import React from 'react';
import {connect} from 'react-redux';
import {Link} from 'react-router';
import * as headerAction from '../../Actions/headerActions';
import * as notificationActions from '../../Actions/notificationActions';

import * as tagProfileActions from '../../Actions/tagProfileActions';

class TagProfile extends React.Component{
static contextTypes = {
router:React.PropTypes.object
};
constructor(props){
super(props)
this.state = {
data:[],
tagName:"",
In:null,
tagId:"",
tagFollowers:0,
isFollowStatus:""

}
this.handleFollow =this.handleFollow.bind(this)
}

handleFollow(e){
//How to Prevent this
console.log(e.keyCode)//Udefined
e.preventDefault();
console.log('Clicked')

}

render(){
console.log(this.state)
return (
<div className="ContainerForTagProfile">
<div className="TagProfileTopSecTion">
<h1>Topic: {this.state.tagName} </h1>
</div>
<div className="StatusBarRealTimeViewsAndFollowButtn">
<div className="ViewCcountBar">
<p>30,18,5222 👁 </p>
</div>
<div className="FollowButtonForTagPRofile">
<button type="button" onClick={(e)=>this.handleFollow(e)}>{this.state.tagFollowers} | {this.state.isFollowStatus}</button>
</div>
</div>
<div className="GallerySectionForTagforfile">


{this.state.data.map(data => {

return (
<Link key={data.id} href="#" target="_blank" to={'/'+data.postId}><div className="SingleImagePost">
<img src={data.thumNailUrl} />
<div className="LiveViewCountBar">
<p>{data.views} 👁 - {data.whoLikedIt.length} ❤</p>
</div>
<div className="LikesBar">
<div className="MakeItCenter">
<div className="ProfileTinyImage">
<img src={data.postOwnerPic} />
</div>
<div className="ProfilePosTOwnerName">
<p>{data.postOwnerFullName}</p>
</div>

</div>
</div>
</div></Link>
)
})}


</div>
</div>
)
}
}

const mapStateToProps = (state) => {
return {
isLoggedIn:state.logInStatus
}
};
const mapDispatchToProps = (dispatch) => {
return {
getTagProfileData:(slug) => dispatch(tagProfileActions.getTagDetails(slug)),
getTagFollowInfo:slug => dispatch(tagProfileActions.getTagFollowInfo(slug)),
toogleSignInOut:bool => dispatch(headerAction.toggleSignPop(bool)),
popErrorNotification:(bool,color,message) => dispatch(notificationActions.popUpNotification(bool,color,message)),
tagProfileFollow:(data) => dispatch(tagProfileActions.tagProfileFollow(data))

}
};

export default connect(mapStateToProps,mapDispatchToProps)(TagProfile)

最佳答案

终于找到解决方案了 刚刚添加了 KeyDown 和 Onclick Evt 并为 KeyBoard 事件抛出了一些 if else 语句..

 handleFollow(e){
e.preventDefault()
if(e.type !== 'keydown'){
//Start Code From here
}

}

关于javascript - 如何在用户单击 Enter 按钮时停止触发按钮 React js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41493237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com