Comments (8)
Another more solid approach is available in the web3-react
repo: the useEagerConnect
hook. The code for that hook looks like this once adapted to use-wallet by using the web3-react instance exposed by useWallet
:
import { useState, useEffect } from 'react';
import { useWallet } from 'use-wallet';
const useEagerConnect = () => {
const { _web3ReactContext, connectors } = useWallet();
const { activate, active } = _web3ReactContext;
const injected = connectors.injected.web3ReactConnector({ chainId: CHAIN_ID });
const [tried, setTried] = useState(false);
useEffect(() => {
injected.isAuthorized().then((isAuthorized) => {
if (isAuthorized) {
activate(injected, undefined, true).catch(() => {
setTried(true);
});
} else {
setTried(true);
}
});
}, []); // intentionally only running on mount (make sure it's only mounted once :))
// if the connection worked, wait until we get confirmation of that to flip the flag
useEffect(() => {
if (!tried && active) {
setTried(true);
}
}, [tried, active]);
return tried;
};
export {
useEagerConnect,
};
from use-wallet.
This works fine for me
const { account, connect, reset, status } = useWallet();
React.useEffect(() => { if(localStorage.getItem("status")==="connected"){ connect("injected") } },[]);
const resetCb = () => { reset(); localStorage.setItem("status", "disconnected"); };
const connectCb = () => { connect("injected").then(() => { localStorage.setItem("status", "connected"); }) }
from use-wallet.
This is for the 'fortmatic' and 'portis' connectors, by the way. Haven't tested the other ones out yet
from use-wallet.
Same happens with meta mask. Would be great if we can connect automatically after user connects their wallet for the first time. This should be possible, on sites like Uniswap they do this.
from use-wallet.
Unfortunately I also encountered the same problem. It is the only reason I am not using this component for my project at the moment.
from use-wallet.
Agreed, it would be a great feature to have! Feel free to open a PR with it, otherwise I’ll have a look after #51 gets merged.
from use-wallet.
Has anyone found a way around this? I just added useWallet to my app, running into same issue
from use-wallet.
I just made a couple of functions like
export function setWalletConnected(value: keyof Connectors | null) {
localStorage.setItem('__WALLET_CONNECTED', JSON.stringify(value));
}
export function isWalletConnected(): keyof Connectors | null {
const val = localStorage.getItem('__WALLET_CONNECTED');
return val ? JSON.parse(val) : null;
}
When a user connects their wallet, I call setWalletConnected('injected')
. Then, when the app is loaded, I just do this
const connectedId = isWalletConnected();
const wallet = useWallet();
if (wallet.status == 'disconnected' && connectedId != null) {
wallet.connect(connectedId)
}
I don't know if it works for other connectors, but Metamask's one works fine.
from use-wallet.
Related Issues (20)
- erc20 Example HOT 1
- [security] Possible issue HOT 1
- Support Frameworks Other Than React HOT 1
- Unexpected character - dist/esm/Cipher.png HOT 1
- Next.js - SyntaxError: Invalid or unexpected token HOT 8
- Hardhat - Uncaught ChainUnknownError: Unknown chain id: 31337
- wallet.chainId defaulting to 1
- support chain do not in the chains list HOT 3
- autoConnect property has bugs!
- useWallet() sometimes return null account value in it's object HOT 1
- Files changed
- explorerUrl inconsistencies
- New use-wallet supporter HOT 8
- Doesn't support React18?
- Throwing error `ChainUnknownError` makes the app crashes
- Add Sepolia as a supported testnet for Ethereum HOT 2
- How does walletconnect use V2 HOT 3
- Unknown chain id: 8453 HOT 1
- ConnectorWalletConnectV2.ts WalletConnect is not a constructor
- When using walletconnectV2, an error is reported HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from use-wallet.