@towns-protocol/react-sdk
Version:
React Hooks for Towns Protocol SDK
39 lines • 1.33 kB
JSX
'use client';
import { useEffect, useState } from 'react';
import { TownsSyncContext } from './internals/TownsSyncContext';
/**
* Provides the sync agent to all hooks usage that interacts with the Towns Protocol.
*
* - If you want to interact with the sync agent directly, you can use the `useSyncAgent` hook.
* - If you want to interact with the Towns Protocol using hooks provided by this SDK, you should wrap your App with this provider.
*
* You can pass an initial sync agent instance to the provider.
* This can be useful for persisting authentication.
*
* @param props - The props for the provider
* @returns The provider
*/
export const TownsSyncProvider = (props) => {
const [syncAgent, setSyncAgent] = useState(() => props.syncAgent);
useEffect(() => {
setSyncAgent(props.syncAgent);
}, [props.syncAgent]);
useEffect(() => {
if (syncAgent) {
syncAgent.start();
}
return () => {
if (syncAgent) {
syncAgent.stop();
}
};
}, [syncAgent]);
return (<TownsSyncContext.Provider value={{
config: props.config,
syncAgent,
setSyncAgent,
}}>
{props.children}
</TownsSyncContext.Provider>);
};
//# sourceMappingURL=TownsSyncProvider.jsx.map