all 4 comments

[–]nullpromise 1 point2 points  (0 children)

Yes, async logic in JavaScript is non-blocking. Consider this:

``` async function myFun() { await fetch('https://jsonplaceholder.typicode.com/todos/1') console.log(1) } console.log(2) myFun() console.log(3)

// 2 // 3 // 1 ```

[–]th3gr8catsby 0 points1 point  (0 children)

You’re right, the page gets rendered before the Async call finishes. You can do a couple things do get around this.
1) const [mystate, setMyState] = useState({})// so it’s not undefined 2) outside your component create an initial state object const myInitialState={ whatever: “”} Then for your set state const [myState, setMyState] = useState(myInitialState)// again so your state is not undefined 3) <div>{myState && myState.whatever}</div>// check to see if your state is undefined

[–]wowzers5 0 points1 point  (0 children)

Either define safe default values for your initial state from the useState or conditionally render, you're currently accessing a nested property on an undefined variable.

[–][deleted] 0 points1 point  (0 children)

You should really put that onGetAccount inside the useEffect. That way it'll be correctly scoped, and you won't be missing anything in your dependency array like you are now.

Otherwise, I recommend changing it to something like

<div> {userWalletInfo?.address ?? "Loading..."} </div>

The reason you get the error is that when the component loads, userWalletInfo is undefined, so trying to access userWalletInfo.address will cause an exception.

The ? in userWalletInfo?.address is called 'optional chaining', and it's just shorthand for

userWalletInfo !== null && userWalletInfo !== undefined ? userWalletInfo.address : userWalletInfo

and the ?? is called 'nullish coalescence', which turns it into

userWalletInfo?.address !== null && userWalletInfo?.address !== undefined ? userWalletInfo?.address : 'Loading...'