React Example for Callbacks
In a React application, you can use callbacks from the @beacon.li/bar
package to manipulate the state of your components based on specific events. This allows you to respond to events triggered by Beacon Bar and update your component's state accordingly.
Example: Using Callbacks to Update State
Here's an example of how to use callbacks to update the state of a React component:
In this example:
We initialize a state variable called
state
usinguseState
. This state will be updated when the callback is triggered.Inside the
useEffect
hook, we define a callback function that updates the state totrue
when it's called.We add the callback using
Beacon.addCallback('your_callback_key', callback)
with a unique key'your_callback_key'
.To ensure cleanup when the component unmounts, we return a function that removes the callback using
Beacon.removeCallback('your_callback_key')
.Finally, we render the component with the state variable to display the current state.
This example demonstrates how to use callbacks from @beacon.li/bar
to manipulate the state of a React component in response to specific events triggered by BeaconBar.
Last updated