Created
December 14, 2021 15:15
-
-
Save Dhaiwat10/7f1089acf4918c6b9ec202cd93b2b41c to your computer and use it in GitHub Desktop.
`useTransaction` hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function App() { | |
const { connectWallet, connected } = useWallet(); | |
const greeterContract = useContract(CONTRACT_ADDRESS, abi); | |
const [greeting, setGreeting] = useState(); | |
const [input, setInput] = useState(); | |
const { execute, error, loading } = useTransaction(greeterContract.setGreeting, [input]); | |
const fetchGreeting = useCallback(async () => { | |
if (connected && greeterContract) { | |
const res = await greeterContract.greeting(); | |
setGreeting(res); | |
} | |
}, [connected, greeterContract]); | |
useEffect(() => { | |
fetchGreeting(); | |
}, [fetchGreeting]); | |
const handleSubmit = async () => { | |
try { | |
await execute(); | |
await fetchGreeting(); | |
} catch (error) { | |
console.log(error); | |
} | |
}; | |
return ( | |
<Container> | |
<VStack> | |
{connected ? ( | |
<> | |
<Heading fontSize="lg">Greeting value</Heading> | |
<Text>{greeting}</Text> | |
<Divider /> | |
<FormControl isInvalid={error}> | |
<VStack> | |
<FormLabel>Set greeting</FormLabel> | |
<Input | |
disabled={loading} | |
value={input} | |
onChange={(e) => setInput(e.target.value)} | |
/> | |
<Button isLoading={loading} onClick={handleSubmit}> | |
Submit | |
</Button> | |
{error && <FormErrorMessage>{error.message}</FormErrorMessage>} | |
</VStack> | |
</FormControl> | |
</> | |
) : ( | |
<Button onClick={connectWallet}>Connect wallet</Button> | |
)} | |
</VStack> | |
</Container> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment