[英]ReactJS ES6 Arrow Function
import React, { 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>
);
}
Can someone explain to me the bolded block of code or convert it into ES5.有人可以向我解释加粗的代码块或将其转换为 ES5。 I think I understand the arrow function but guess not.
我想我理解箭头 function 但猜不透。 What I know about arrow function is... You use it like this,
我对箭头 function 的了解是......你这样使用它,
const timesTwo = (a) => { return a*2 } or
const timesTwo = a => a*2 or
const timesTwo = a =>( a*2)
I don't understand this piece of code () => setCount(count + 1)
I understand that count is going to increase as soon as user clicks but is it same as writing我不明白这段代码
() => setCount(count + 1)
我知道一旦用户点击,count 就会增加,但它和写作一样吗
function setCount(){
count: count +1;
}
Thank You, I appreciate your time.谢谢你,我很感激你的时间。
The function you have in the onClick event is equivalent to:您在 onClick 事件中拥有的 function 相当于:
function() {
return setCount(count + 1);
}
But you don't need to return the setCount value, so it's better to change it to但是你不需要返回setCount值,所以最好改成
()=> { setCount(count + 1); }
Or just:要不就:
function() {
setCount(count + 1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.