score:3
Accepted answer
This is correct because event keydown is attached to an element. The default context come back useful when you are working with inputs. When you need a global event (as events are propagated top/down) you can rely on window object or document object:
componentDidMount(){
document.addEventListener('keydown', (event) => {
const keyName = event.key;
alert('keydown event\n\n' + 'key: ' + keyName);
});
}
componentWillUnmount(){
// Remove the listener before unmount
}
Source: stackoverflow.com
Related Query
- onKeyDown event not working on divs in React
- onKeyDown Event in React with TypeScript
- React - onKeyDown event
- Why onKeyDown event has no effect in React app?
- React event onKeyDown only works if I press the key inside an input
- OnChange event using React JS for drop down
- Unable to access React instance (this) inside event handler
- Typescript: React event types
- How can I prevent event bubbling in nested React components on click?
- React onClick - pass event with parameter
- What is the best way to trigger onchange event in react js
- React JS onClick event handler
- How to use onClick event on react Link component?
- React passing parameter via onclick event using ES6 syntax
- React - Prevent Event Trigger on Parent From Child
- Event Handlers in React Stateless Components
- Wrong React hooks behaviour with event listener
- React onClick event on component
- Get object data and target element from onClick event in react js
- React Synthetic Event distinguish Left and Right click events
- React useState hook event handler using initial state
- react long press event
- Remove Event Listener On Unmount React
- React - how to capture only parent's onClick event and not children
- Simulate click event on react element
- React event onMouseLeave not triggered when moving cursor fast
- Get reference to React component in event handler
- React and blur event
- Image onLoad event in isomorphic/universal react - register event after image is loaded
- onChange event for React child component to update state
More Query from same tag
- How to use node-sass in react-static
- Building Twitter Like Dialogbox for input
- Output object name other than React with jsx syntax
- How to disable an active item after dispatch?
- React.js class programming not able to stop count down timer
- How to use .dispatch in react redux?
- I need to subtract the count and update the remaining count
- Access token appears to be incorrect format using auth0-react library
- Overflow-x scroll doesn't work properly on mobile
- How to show in a list, visible markers in the google map with React
- Cannot read property 'localeCompare' of undefined
- How to Test Private Routing with React & React-Router
- React Router <Component/> vs {Component}
- Cannot read properties of undefined (reading 'preventDefault')
- Javascript get Date format in H:M:S using react js
- Function component keeps rerendering over and over
- Generate a unique id in react that persist
- React & Reselect selector claims state is the same after update
- Filter nested array of objects
- React Error: "Cannot update during an existing state transition"
- Redux reducer to remove an object from a list by name
- I need to have a dynamic grid like masonry grid based on scores from json
- material UI Menu not appearing right place when button align right or left
- display:inline-block is not working in Bootstrap
- React Testing Library - TypeError: actImplementation is not a function
- How to convert an object into an array of objects?
- JavaScript Map function to use generic keys within array of objects
- Filter array based on multiple filters
- How do I use MUI's shadows?
- How to draw an SVG onclick in React