add listener for account change
This commit is contained in:
parent
f1eafb8d40
commit
58117bf5e8
18
src/dapp.js
18
src/dapp.js
|
@ -37,13 +37,17 @@ class App extends React.Component {
|
|||
const network = process.env.REACT_APP_NETWORK || 'ropsten'
|
||||
this.setCurrencies(network)
|
||||
this.setGraphClient(network)
|
||||
this.grabAddress()
|
||||
}
|
||||
|
||||
grabAddress = () => {
|
||||
if (window.ethereum) {
|
||||
this.accountListener()
|
||||
const { selectedAddress: account } = window.ethereum
|
||||
if (account) this.setState({ account })
|
||||
} else {
|
||||
console.log('window.ethreum not found :', {window})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
setCurrencies = async network => {
|
||||
|
@ -75,6 +79,18 @@ class App extends React.Component {
|
|||
this.setState({ loading: false })
|
||||
}
|
||||
|
||||
accountListener = () => {
|
||||
let self = this
|
||||
try {
|
||||
window.ethereum.on('accountsChanged', function (accounts) {
|
||||
const [account] = accounts
|
||||
self.setState({ account })
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('accountsChanged listener : ', {error})
|
||||
}
|
||||
}
|
||||
|
||||
enableEthereum = async () => {
|
||||
try {
|
||||
await window.ethereum.enable()
|
||||
|
|
Loading…
Reference in New Issue