我需要帮助将这个精巧的代码转换为 react.js
I need help converting this svelte code to react.js
我在网上找到了这段精巧的代码片段,我一直在尝试在 react.js 中重新创建它,我知道它与 componentDidMount() 有关,但我不确定如何去做.
<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>
功能组件中的挂载效果是没有依赖关系的效果:
const P = () => {
const [value, setValue] = useState();
useEffect(() => {
getETHPrice().then(setValue);
}, []);
return <><h1><p>{`${(value||0).toFixed(2)}USD`}</p></h1></>;
}
我在网上找到了这段精巧的代码片段,我一直在尝试在 react.js 中重新创建它,我知道它与 componentDidMount() 有关,但我不确定如何去做.
<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>
功能组件中的挂载效果是没有依赖关系的效果:
const P = () => {
const [value, setValue] = useState();
useEffect(() => {
getETHPrice().then(setValue);
}, []);
return <><h1><p>{`${(value||0).toFixed(2)}USD`}</p></h1></>;
}