Advertisement
josephkagimu1

Select Radio button when enter key is pressed

Mar 30th, 2022
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. import { useState, useEffect } from 'react';
  2.  
  3. const HandleKeypress = () => {
  4.  
  5. const [itWorks, setItWorks] = useState(true)
  6.  
  7. useEffect(() => {
  8. document.addEventListener('keypress', (e) => {
  9. if (e.code === 'Enter') setItWorks(!itWorks)
  10. e.preventDefault();
  11. })
  12. }, [])
  13.  
  14. return (
  15. <div>
  16. <p>{itWorks ? 'It works!' : 'It does not'}</p>
  17. <button onClick={() => setItWorks(!itWorks)} >Press me</button>
  18.  
  19. <input type='radio' aria-selected onKeyPress={() => this.HandleKeypress } />
  20.  
  21. </div>
  22. )
  23. }
  24.  
  25. export default HandleKeypress;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement