Resolved: Can not get .bns name from userSession.loadUserData() in React app using the Stacksjs package

In this post, we will see how to resolve Can not get .bns name from userSession.loadUserData() in React app using the Stacksjs package

Question:

I am building an application for the Stacks Blockchain with Clarity smart contract language and Stacksjs on the frontend. From my React UI, I am unable to get the .bns name from the userSession object. From what I researched, it should be under userSession.loadUserData().username.
Below is the code snippet:

Best Answer:

The username property was recently deprecated in favor of letting the application handle retrieving BNS names. You can use this example file to see how you might pull the BNS name if you have the user’s address.
This was pulled from this commit that used to live in Hiro’s example Todo application, although it looks like it has since been removed.
https://github.com/hirosystems/todos/pull/104/files#diff-41aaffe0a6b7fb309928aa1e15b50b58bc3348249614110efe07cf89da61c7a2

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com