gpt4 book ai didi

javascript - 使用 ES6 的 React Native Docs 示例

转载 作者:行者123 更新时间:2023-11-30 19:10:01 25 4
gpt4 key购买 nike

以下代码来自 React Native 文档。如您所见,有两个带有粗箭头的函数减速。我理解普通括号与包含 .... previousState => ({ 等的行的用法。这里需要使用 () 因为它返回一个对象文字。但是,我无法理解为什么我们在 setInterval 的回调函数中使用“(”。我的意思是这一行:setInterval(() => ( ...。为什么我们不写像 setInterval(() => { .....

class Blink extends Component {

componentDidMount() {

//HEART OF THE QUESTION. Why do we use "(" below, instead "{". Do we need to return for setInterval or just define a function to run ?
setInterval(() => (

//Here, "(" is normal because it returns object literal
this.setState(previousState => ({
isShowingText: !previousState.isShowingText
}))
), 1000);
}

//....

}

最佳答案

setInterval 不需要返回值。这两个都可以很好地工作:

setInterval(() =>
this.setState(previousState => ({
counter: previousState.counter + 1 || 1
})),
1000);

还有这个(在我看来,没有括号就不好看):

setInterval(() => {
this.setState(previousState => ({
counter: previousState.counter + 1 || 1
}));
}, 1000);

通常多行返回需要括号,这里有一个很好的解释: http://jamesknelson.com/javascript-return-parenthesis/

关于javascript - 使用 ES6 的 React Native Docs 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58592517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com