Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

useConnect: Make connect function undefined until connector is ready #60

Merged
merged 5 commits into from
Mar 13, 2024

Conversation

bisgardo
Copy link
Contributor

@bisgardo bisgardo commented Mar 12, 2024

This prevents the connect function from being called before WalletConnectionProps.activeConnector is ready as the call would then fail. The expectation is that the button/function invoking connect checks that doing so is valid. With this change that check is now more direct and less error-prone.

This a only barely a breaking change because the expected usage of passing the function to onClick of a button keeps working without modification. The difference is that now the click is a noop if the call is invaild instead of an exception being thrown. And checking validity is just the truthiness value of connect. In other words you can now just do

<Button type="button" onClick={connect} disabled={!connect}>Connect</Button>

Nice and simple.

An alternative solution would be to return an additional boolean isReadyToConnect, but that would not allow the type system to prevent the call like it does with this one.

Let `connection` function be `undefined` if `connector` is. This prevents the function from being called before `WalletConnectionProps.activeConnector` is ready which would fail anyway. The expectation before was that the button/function invoking `connect` would check this itself, but making it explicit in the type seems less error-prone.

I do not really consider this a breaking change because the type is being strictly expanded (to include `undefined`) and doesn't break any existing correct code. If one did pass the `connect` function to other functions (which would be weird), the type signatures of those functions might have to be updated though.

An alternative solution would be to return an additional boolean `isReadyToConnect`, but that would not allow the type system to prevent the call like it does with this one.
@bisgardo bisgardo force-pushed the react-components/nullable-connect branch from e62c133 to df59b98 Compare March 12, 2024 12:29
Also "migrated" the samples though no migration was strictly necessary.
@bisgardo bisgardo merged commit b5ebf2b into main Mar 13, 2024
1 check passed
@bisgardo bisgardo deleted the react-components/nullable-connect branch March 13, 2024 13:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants