11. Building Custom React Hooks
Activity 11.1: Build a Custom Keyboard Input Hook
Solution:
Perform the following solution steps to complete this activity:
- As a first step, create a
hooks/use-key-event.js
file. This file will hold the custom Hook function. - Create the
useKeyEvent
Hook function in the newly addeduse-key-event.js
file. Also, export theuseKeyEvent
function so that it can be used in other files (it will be used in theApp
component later):function useKeyEvent() { // logic to be added... } export default useKeyEvent;
- Move the
useEffect()
import and call (and all the logic inside of it) from theApp
component body to theuseKeyEvent
function body:import { useEffect } from 'react'; function useKeyEvent() { useEffect(() => { function keyPressedHandler(event) { const pressedKey = event.key; if (!['s', 'c&apos...