简体   繁体   English

使用 ES6 反应原生文档示例

[英]React Native Docs Example with ES6

The below code is from React Native Docs.以下代码来自 React Native Docs。 As you can see, there are two function declerations with fat arrows.如您所见,有两个带有粗箭头的 function declerations。 I understand the usage of normal parenthesis with the line containing .... previousState => ({ etc. Here use of () is needed because it returns an object literal. However, I can't understand why do we use "(" with the callback function of the setInterval . I mean this line: setInterval(() => (... . Why we do not write like setInterval(() => {.....我理解在包含.... previousState => ({等的行中使用普通括号。这里需要使用 (),因为它返回 object 文字。但是,我不明白为什么我们使用“(”使用setInterval的回调 function 。我的意思是这一行: 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 doesn't need a return. setInterval 不需要返回。 Both of these will work perfectly fine:这两个都可以正常工作:

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

and this (which is not pretty without parenthesis, in my opinion):还有这个(在我看来,没有括号就不漂亮了):

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

Usually parenthesis is needed for multiline returns, here is a good explanation: http://jamesknelson.com/javascript-return-parenthesis/通常多行返回需要括号,这里有一个很好的解释: http://jamesknelson.com/javascript-return-parenthesis/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM