React Example for Callbacks
Example: Using Callbacks to Update State
import { Beacon } from '@beacon.li/bar';
import { useEffect, useState } from 'react';
function Component() {
// Initialize a state variable
const [state, setState] = useState(false);
useEffect(() => {
// Define a callback function
const callback = (userId, metaData) => {
// Update the state when the callback is triggered
setState(true);
};
// Add the callback with a unique key
Beacon.addCallback('your_callback_key', callback);
// Clean up by removing the callback when the component unmounts
return () => {
Beacon.removeCallback('your_callback_key');
};
}, [setState]);
// Render your component with the state
return (
<div>
<p>State: {state ? 'True' : 'False'}</p>
</div>
);
}
export default Component;Last updated