我目前正在学习React中的钩子概念,并试图理解下面的例子.
import { useState } from 'react';
function Example() {
// Declare a new state variable, which we'll call "count"
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
上面的示例在处理程序函数参数本身上递增计数器.如果我想修改事件处理函数内的计数值怎么办?
考虑下面的例子
setCount = () => {
//how can I modify count value here. Not sure if I can use setState to modify its value
//also I want to modify other state values as well here. …Run Code Online (Sandbox Code Playgroud) 如何在新的reactjs钩子api中调用restApi.并使用useEffects和useState重用数据?我想只重用组件之间的数据.
import { useState, useEffect } from "react";
export default function getAdvice(url) {
fetch(`http://api.adviceslip.com/advice`)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.log(err)
}
Run Code Online (Sandbox Code Playgroud)