Hi @cdevl3749 , how is this supposed to work? You’re initialising connected as true, then immediately setting it to false (effective with the next render) and that was it.
Where and when would you actually determine whether you’re “connected” or not?
I have my page where you have to connect with Metamask if you want to see the data and it works very well and I would like to do the same with the “FAQ” button so that you can see it only if you are connected to Metamask
The code of my button where I would like to make the condition
Thanks for the clarification. So that’s a different FAQ Button Component located somewhere else where you want to do this check? In this case, you might try passing the state (as set with saveState) to that component, and check if it’s actually set.
If this is not feasible directly, you might have a look at the useContext() hook:
The snippet posted by @averyjonesss888 diverges from yours in a couple of aspects, but the crucial bit might be that they’re setting the connected state depending on the ethereum response:
setIsConnected(accounts.length > 0);
So for the purpose of testing, try to just
setIsConnected(true);
Also carefully review their snippet. They have the connected state the other way round (justifiably IYAM), and they’re using a dedicated effect for the contact section. If you’re using this check a lot, this would mean a new request for each such component, where you might actually just check the state you already saved inside the Wallet component.
PS: AFAICT you might actually drop the useState() hook altogether, and infer connected state from the state you stored somewhere above with saveState().
Yes that line (and only that line BTW) was supposed to be replaced for the purpose of testing only. If this works, you can go ahead and implement an actual connection check… that’s what accounts.length > 0 is supposed to do I guess.
I’m not familiar with the ethereum API myself though. @averyjonesss888 maybe you can elaborate?
I have a page where when I connect to Metamask it displays the data from my smart contract and I would simply like to ensure that when I connect it displays the “FAQ” button otherwise not.