'I need help converting this svelte code to react.js
I found this code svelte code snippet online and i have been trying to recreate it in react.js, i know it has something to do with componentDidMount(), but im not sure on how to go about it.
<script>
import { onMount } from "svelte";
import { getETHPrice } from "../utils/getETHPrice";
let value;
onMount(async () => {
value = await getETHPrice();
});
</script>
<h1>Current Data Feed for MATIC/USD</h1>
<p>
$ {(1 * value).toFixed(2)} USD
</p>
Solution 1:[1]
Mount effects in functional components are effects with no dependencies:
const P = () => {
const [value, setValue] = useState();
useEffect(() => {
getETHPrice().then(setValue);
}, []);
return <><h1><p>{`${(value||0).toFixed(2)}USD`}</p></h1></>;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
