Simple pubsub hook for react

BlogReacthooks

April 10, 2019

While working with making some dom elements movable in for a project I am working on, I found that I wanted to have a single handler for mouse move instead of having the individual components that I needed to move. This would facilitate moving multiple components at once, and also ensured that I could not move off of the component if I moved the mouse to fast.

To facilitate this, I made a simple pubsub hook that let me register subscribers to a channel, and publish to an event. In the codesandbox below it is simply incrementing a counter.

https://codesandbox.io/s/kl2jkpvo5