Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react keydown event listener freecodecamp

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      message: ''
    };
    this.handleEnter = this.handleEnter.bind(this);
    this.handleKeyPress = this.handleKeyPress.bind(this);
  }
  // change code below this line
  componentDidMount() {
    document.addEventListener('keydown', handleKeyPress);
  }
  componentWillUnmount() {
    document.removeEventListener('keydown', handleKeyPress);
  }
  // change code above this line
  handleEnter() {
    this.setState({
      message: this.state.message + 'You pressed the enter key! '
    });
  }
  handleKeyPress(event) {
    if (event.keyCode === 13) {
      this.handleEnter();
    }
  }
  render() {
    return (
      <div>
        <h1>{this.state.message}</h1>
      </div>
    );
  }
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: filter array and get index of num 
Javascript :: tailwind intenseness react 
Javascript :: delayed usestate double click 
Javascript :: axios xmlhttpReq 
Javascript :: animateOut: "slideOutUp", animateIn: "slideInUp", not working 
Javascript :: dollar sign brackets javascript 
Javascript :: make a circle in javascript 
Javascript :: react show new app 
Javascript :: python to javascript converter 
Javascript :: normalizedList.flatMap is not a function vue draggable 
Javascript :: render(<App /); const linkElement = screen.getByText(/learn react/i); expect(linkElement).toBeInTheDocument(); 
Javascript :: Javascript - The file size is measured in bytes 
Javascript :: javascript get token from query string 
Javascript :: pasar datos al redirect js node 
Javascript :: change the input feild name when the div contaoining that field is cloned using jquery 
Javascript :: how to convert names to initials 
Javascript :: javascript localstorage reference 
Javascript :: jquery validate required false with additional function 
Javascript :: Spread Syntax for function 
Javascript :: caeser psypher javascript 
Javascript :: khai bao bien trong javascript 
Javascript :: get json model from another component in sapui5 
Javascript :: everything be true freecodecamp 
Javascript :: node.js how to install a custom version of packgage 
Javascript :: object mapper pretty write as string 
Javascript :: active state of nav while scrolling 
Javascript :: Jboss heap dump 
Javascript :: nodejs s3 read 
Javascript :: crypto digest node.js 
Javascript :: what is runtime in javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =