diff --git a/src/components/balance.js b/src/components/balance.js index 219daa7..3e05e9d 100644 --- a/src/components/balance.js +++ b/src/components/balance.js @@ -3,83 +3,62 @@ */ // Global npm libraries -import React from 'react' +import React, { useState } from 'react' import { Container, Row, Col, Form, Button, Spinner } from 'react-bootstrap' -// let _this +function GetBalance (props) { + const { wallet } = props -class GetBalance extends React.Component { - constructor (props) { - super(props) + // State + const [balance, setBalance] = useState('') + const [textInput, setTextInput] = useState('') - this.state = { - balance: '', - textInput: '', - wallet: props.wallet - } - - // Bind 'this' to event handlers - this.handleGetBalance = this.handleGetBalance.bind(this) - - // _this = this - } - - render () { - return ( - - <> - - - -
- - Enter a BCH address to check the balance. - this.setState({ textInput: e.target.value })} /> - - - -
- -
-
- - - {this.state.balance} - - -
- - ) - } - - async handleGetBalance (event) { + // Button click handler + const handleGetBalance = async (event) => { try { - const textInput = this.state.textInput - // Exit on invalid input if (!textInput) return if (!textInput.includes('bitcoincash:')) return - this.setState({ - balance: (Retrieving balance... ) - }) + setBalance(Retrieving balance... ) - const balance = await this.state.wallet.getBalance({ bchAddress: textInput }) + const balance = await wallet.getBalance({ bchAddress: textInput }) console.log('balance: ', balance) - const bchBalance = this.state.wallet.bchjs.BitcoinCash.toBitcoinCash(balance) + const bchBalance = wallet.bchjs.BitcoinCash.toBitcoinCash(balance) - this.setState({ - balance: `Balance: ${balance} sats, ${bchBalance} BCH` - }) + setBalance(`Balance: ${balance} sats, ${bchBalance} BCH`) } catch (err) { - this.setState({ - balance: (

Error: {`${err.message}`}

) - }) + setBalance(

Error: {`${err.message}`}

) } } + + return ( + <> + + + +
+ + Enter a BCH address to check the balance. + setTextInput(e.target.value)} /> + + + +
+ +
+
+ + + {balance} + + +
+ + ) } export default GetBalance