Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

add listener in react

// You can do this:
function App() {
  	return (
      	<div onChange={() => console.log('You clicked me')}>
      	hello!
      	</div>
    )
}

// or...
import {useRef, useEffect} from 'react'

const wrapperRef = useRef(null)

useEffect(() => {
  	function callback() {
      	console.log('You clicked me!')
    }
  
  	wrapperRef.current?.addEventListener('click', callback)
  
  	return () => wrapperRef.current?.removeEventListener('click', callback)
}, [])

function App() {
  	return (
      	<div ref={wrapperRef}>
      	hello!
      	</div>
    )
}
Comment

react event listener

// it a component
import React from 'react';

class App extends React.Component {
      //call function (ECMAScript 5) from tag input:text
  handleChange = e => {
    //
    console.log(`${e.target.value}`)
  }
  render() { 
    return (
      <div className="App">
        <input type="text" name="input" id="" placeholder="" onChange={this.handleChange}/>
      </div>
    );
}
}

export default App;
Comment

how to use an event listener in a react js component

class Button extends React.Component {
  scream() {
    alert('AAAAAAAAHHH!!!!!');
  }

  render() {
    return <button onClick={this.scream}>AAAAAH!</button>;
  }
}

ReactDOM.render(<Button />, document.getElementById('app'));
Comment

PREVIOUS NEXT
Code Example
Javascript :: array empty strings 
Javascript :: jquery onchange event 
Javascript :: how to global a variable in javascript 
Javascript :: adding cors parameters to extjs ajax 
Javascript :: puppeteer example multiple file upload 
Javascript :: google apps script getsheetbyname 
Javascript :: how to use jquery plugins in angular 8 
Javascript :: add material angular 
Javascript :: react native dropdown 
Javascript :: brain.js 
Javascript :: radio button schema mongoose 
Javascript :: When JavaScript was invented month?* 
Javascript :: chart js more data than labels 
Javascript :: javascript got device ip 
Javascript :: javascript stringify blob 
Javascript :: nodejs module 
Javascript :: jquery event delegation 
Javascript :: js insert in array 
Javascript :: reacts mos twanted 
Javascript :: material ui change icon size on xs screen 
Javascript :: switch new date getday javascript 
Javascript :: length of array 
Javascript :: on enter to tab javascript 
Javascript :: qr code terminal npm 
Javascript :: how to use react-native-vector-icons 
Javascript :: addition of time in javascript 
Javascript :: global catch in javascript 
Javascript :: s3.getobject nodejs example 
Javascript :: javascript get first element of array 
Javascript :: regex checker 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =