-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
onConnected not fired on page load #176
Comments
@ekzyis thanks for bringing this up - the example does not consider the case where the user is already connected on the site, and where Bitcoin Connect has already connected before the React effect runs. I think we need something like this: useEffect(() => {
// init bitcoin connect to provide webln
const [isConnected, onConnected, requestProvider] = await import('@getalby/bitcoin-connect-react').then(
(mod) => [mod.isConnected, mod.onConnected, requestProvider]
);
// handle case where Bitcoin Connect is already connected
// requestProvider will not launch a modal because a provider is already available.
if (isConnected()) {
(async () => {
window.webln = await requestProvider();
})();
}
// standard case (when user connects sometime in the future)
const unsub = onConnected((provider) => {
window.webln = provider;
});
return () => {
unsub();
};
}, []); @bumi @reneaaron do you have any thoughts? (Note: this is not about specifically about setting the global webln object, but handling both cases for getting a provider without unexpectedly opening any modals) |
Your code example works great! Nitpick: As your code is, it wouldn't work since you use And since useEffect(() => {
const unsub = []
async function effect () {
const [isConnected, onConnected, onDisconnected, requestProvider] = await import('@getalby/bitcoin-connect-react').then(
(mod) => [mod.isConnected, mod.onConnected, mod.onDisconnected, mod.requestProvider]
)
if (isConnected()) {
// requestProvider will not launch a modal because a provider is already available.
// TODO but it might for wallets that must be unlocked?
const provider = await requestProvider()
setProvider(provider)
}
unsub.push(onConnected(async (provider) => {
setProvider(provider)
const info = await provider.getInfo()
setInfo(info)
}))
unsub.push(onDisconnected(() => {
setProvider(null)
setInfo(null)
}))
}
effect()
return () => unsub.forEach(fn => fn())
}, [setProvider]) Also see my TODO:
I haven't tested this yet but wouldn't it try to unlock? Or would it not since a locked wallet is not considered connected? |
@ekzyis regarding the TODO: Bitcoin Connect saves your last connection to localStorage, and will automatically call enable on the provider as soon as the library is initialized, so you will get a popup anyway. Until enable() is called and the user allows any prompts, |
I will review this with Bumi and Rene and see if we go ahead with this updated documentation, or make a simpler way for devs to achieve this functionality. |
I talked with @bumi
I propose to change |
Afaict, I used
onConnected
as described in the docs (insideuseEffect
) but it does not fire on page load.The docs mention this usage:
See my code in stackernews/stacker.news#715
Here is a showcase video:
2023-12-28.23-42-06.mp4
The text was updated successfully, but these errors were encountered: