Closed mcintyre94 closed 1 year ago
I think the event behaviour is correct, update
events fire on update and not on initial set. There is a connect
event which includes initial data and ensures window.xnft is available and functional.
The problem here is that most of the hooks dont wait for the connect
event to make sure initial values are set iE window.xnft.publicKeys
.
So instead of a timeout the hook should listen to the connect
event:
export function usePublicKeys(): PublicKey {
const [publicKeys, setPublicKeys] = useState(window.xnft.publicKeys);
console.log('upk initial state', publicKeys)
useEffect(() => {
window.xnft.on("publicKeysUpdate", () => {
setPublicKeys(window.xnft.publicKeys);
});
window.xnft.on("connect", () => {
setPublicKeys(window.xnft.publicKeys);
});
}, [setPublicKeys]);
return publicKeys;
}
Updated hooks here: https://github.com/coral-xyz/xnft-quickstart/pull/33
The
usePublicKeys
hook in the quickstart relies on an emittedpublicKeysUpdate
event (https://github.com/coral-xyz/xnft-quickstart/blob/master/src/hooks/xnft-hooks.tsx#L24):This event doesn't seem to be emitted when
window.xnft.publicKeys
transitions fromundefined
to being setYou can see this by using this modified hook:
In addition to the existing event listener, this adds a check of
window.xnft.publicKeys
5s after load, and also adds some debug loggingWhen I run an xnft calling this hook I see in the console:
In an app displaying the result of this hook I can also see that the value doesn't update before the timeout after 5s
So the
window.xnft.publicKeys
is correctly updating, but the event handlerwindow.xnft.on("publicKeysUpdate", ...
is not being called. My best guess is that this is a bug in backpack where the event is not being emitted correctlyTested on the current latest build, 0.5.2-latest-4128