Hey man, awesome in depth article! I tried to do the same thing for some side project. I used the gamepads library as an abstraction layer for the Gamepad API and used that in my React app.
Instead of storing all gamepad buttons in state, I passed a mapping to my custom hook that allows you to handle button events. This way, you can handle button presses just like onClick events. I also use a React Context to store the registered gamepads.
This is the custom hook for registering listeners on a gamepad:
I never really finished this and I remember it being a little buggy, but I like how declarative this approach is. It allows you to keep the event handling in one place, away from the logic of your component. I'm wondering if this would be better for performance, compared to rendering the app with the gamepad state all the time?
On a side note, the guy from the gamepads library also published gamepad-icons, which makes it easy to display gamepad button icons in your UI. It's also possible to detect the type of controller and show the matching icons.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hey man, awesome in depth article! I tried to do the same thing for some side project. I used the gamepads library as an abstraction layer for the Gamepad API and used that in my React app.
Instead of storing all gamepad buttons in state, I passed a mapping to my custom hook that allows you to handle button events. This way, you can handle button presses just like onClick events. I also use a React Context to store the registered gamepads.
This is the custom hook for registering listeners on a gamepad:
And in your component, you use it like this (BUTTONS is a standard mapping provided by the gamepads library):
I never really finished this and I remember it being a little buggy, but I like how declarative this approach is. It allows you to keep the event handling in one place, away from the logic of your component. I'm wondering if this would be better for performance, compared to rendering the app with the gamepad state all the time?
On a side note, the guy from the gamepads library also published gamepad-icons, which makes it easy to display gamepad button icons in your UI. It's also possible to detect the type of controller and show the matching icons.