Ⅰ react 事件怎麼調用多個函數
觸摸事件:onTouchCancel\onTouchEnd\onTouchMove\onTouchStart
(只會在移動設備上接受)
鍵盤事件:onKeyDown\onKeyPress\onKeyUp
剪切事件:onCopy\onCut\onPaste
表單事件:onChange\onInput\onSubmit
焦點專事件:onFocus\onBlur
UI元素:onScroll(移動設備是手指滾動屬和PC的滑鼠滑動)
滾動事件:onWheel(滑鼠滾輪)
滑鼠類型:onClick\onContextMenu(右鍵)\onDoubleClick\onMouseDown\onMouseEnter\
onMouseLeave\onMouseMove\onMouseOut\onMouseOver\onMouseUp
onDrag\onDrop\onDragEnd\onDragEnter\onDragExit\onDragLeave\onDragOver\onDragStart
return{...一堆事件處理專函屬數}
}();
AdmanageRE.Monitor=function()
{
//...一堆事件處理函數
}();
</script>
<body onload="AdmanageRE.Monitor.getaaa()" onmouseover="AdmanageRE.Monitor.MouseOver1(event)" onmousedown="AdmanageRE.Monitor.MouseClick1()" onkeydown="AdmanageRE.Monitor.MouseKeydown(event)">
<h3 onclick="AdmanageRE.Monitor.MouseClick2(event)" onmouseover="AdmanageRE.Monitor.MouseOver2()"></h3>
Ⅲ react 怎麼處理後台字元串事件
使用React元素處理事件與處理DOM元素上的事件非常相似。不過有一些語法上的差異:
react事件使用駝峰命名法,而不是全部小寫命名。
使用JSX你傳遞一個函數作為事件處理程序,而不是一個字元串。
例如,HTML:
<button onclick="activeLasers()">
Active Lasers
</button>
在React中略有不同:
<button onClick={activateLasers}>
Active Lasers
</button>
另一個區別是,你不能返回false來防止React中的默認行為。 您必須顯式調用preventDefault。 例如,使用純HTML,為了防止打開新頁面的默認鏈接行為,您可以寫:
<a href="#" onclick="console.log('The link was clicked'); return false;">
Click me
</a>
在React中,這可以改為:
function ActiveLink() {
function handleClick(e) {
e.preventDefault();
console.log('The link was clicked');
}
return (
<a href='#' onClick={handleClick}>
click me
</a>
);
}
Ⅳ reactJs 將js交互和事件全部寫在componentDidMount裡面可以嗎
react實現虛擬dom的好處就復是可直接操作dom而節制省了獲取dom的時間和資源成本,交互如果都寫在了componentDidMount中,就相當於還是在用jquery在寫代碼,況且狀態的更新只有componentDidMount是完全不夠實現復雜的交互,充分合理的使用它的生命周期才能體會到其中之美。至於state的管理,大型的項目你可以考慮flux,rex來幫助你管理,小型的項目可以在寫代碼之前,思考清楚組件之間的數據共享問題之後再動手。
Ⅳ reactjs中a標簽有什麼事件嗎
CANNOT GET /user/shaochuancs 是因為 server 沒配 history API fallback。
如果要讓 dangerouslySetInnerHTML 的 a 標簽支持 history push,可以手工給這些 a 標簽綁定專 click 事件屬:
componentDidMount() {
const links = this.content.querySelectAll('a');
// 給 a 標簽綁定 click 事件,自己處理跳轉。
}
Ⅵ React 事件處理為什麼推薦調用 bind
官方推薦的是在constructor中bind,或者箭頭函數class屬性初始化語法。
class Toggle extends React.Component {
constructor(props) {
super(props);
this.state = {isToggleOn: true};
// This binding is necessary to make `this` work in the callback
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState(prevState => ({
isToggleOn: !prevState.isToggleOn
}));
}
render() {
return (
<button onClick={this.handleClick}>
{this.state.isToggleOn ? 'ON' : 'OFF'}
</button>
);
}
}
class LoggingButton extends React.Component {
// This syntax ensures `this` is bound within handleClick.
// Warning: this is *experimental* syntax.
handleClick = () => {
console.log('this is:', this);
}
render() {
return (
<button onClick={this.handleClick}>
Click me
</button>
);
}
}
Ⅶ 如何把jsx文件轉成js文件如babel等工具怎麼轉換
return{...一堆事件處回理函答數}
}();
AdmanageRE.Monitor=function()
{
//...一堆事件處理函數
}();
</script>
<body onload="AdmanageRE.Monitor.getaaa()" onmouseover="AdmanageRE.Monitor.MouseOver1(event)" onmousedown="AdmanageRE.Monitor.MouseClick1()" onkeydown="AdmanageRE.Monitor.MouseKeydown(event)">
<h3 onclick="AdmanageRE.Monitor.MouseClick2(event)" onmouseover="AdmanageRE.Monitor.MouseOver2()"></h3>
Ⅷ 如何實現ReactJS 監聽頁面滾動事件
實現ReactJS 監聽頁面滾動事件,代碼如下:
export class Example extends Component{ scrollHandler = this.handleScroll.bind(this); componentDidMount() { window.addEventListener('scroll', this.scrollHandler); } _handleScroll(scrollTop) { console.log(scrollTop) //滾動條距專離頁面的高屬度 } } handleScroll(event) { let scrollTop = event.srcElement.body.scrollTop; this._handleScroll(scrollTop); }}
Ⅸ react js 怎樣綁定鍵盤敲擊回車事件
class Demo extends Components{
componentDidMount(){
document.addEventListener("keydown",this.handleEnterKey);
}
componentWillUmount(){
document.removeEventListener("keydown",this.handleEenterKey);
}
handleEnterKey = (e) => {
if(e.keyCode === 13){
//do somethings
}
}
render(){
}
}

擴展抄資料
React獲取表單值襲
1、監聽表單的改變事件 onChange
2、在改變的事件里獲取表單輸入的值 通過事件對象獲取值 e.target.value
3、把表單輸入的值賦值給username this.setState({})
4、點擊按鈕的時候獲取state裡面的username this.state.username
<input onChange={this.inputChange} onClick={this.getData} />
Ⅹ React中如何優雅的捕捉事件錯誤
1. EerrorBoundary
EerrorBoundary是16版本出來的,有人問那我的15版本呢,我不聽我不聽,反正我用16,當然15有unstable_handleError。
關於EerrorBoundary官網介紹比較詳細,這個不是重點,重點是他能捕捉哪些異常。
Error boundaries在rendering,lifeCyclemethod或處於他們樹層級之下的構造函數中捕獲錯誤
哦,原來如此。 怎麼用
.Component{constructor(props){super(props);this.state={hasError:false};
}
componentDidCatch(error,info){//DisplayfallbackUI
this.setState({hasError:true});//
logErrorToMyService(error,info);
}
render(){if(this.state.hasError){//
return<h1>Somethingwentwrong.</h1>;
}returnthis.props.children;
}
}
<ErrorBoundary><MyWidget/></ErrorBoundary>