React elements and fat arrow functions

后端 未结 1 1930
猫巷女王i
猫巷女王i 2021-02-04 16:27

In the Redux examples, the syntax used is:

const App = () => (
  
1条回答
  •  日久生厌
    2021-02-04 17:10

    TL;DR

    Your first example is more or less equivalent to:

    var App = function() { return 
    ...
    ; };

    Your second is more or less equivalent to:

    var App = function() { 
    ...
    ; };

    React is probably complaining that nothing is being returned in the second example.

    Slightly Longer Version

    Let's take React out of the equation. In es6 you can create a fat arrow function like this:

    const getWord = () => {
      return 'unicorn';
    }
    

    And we're given a shortcut to do the same thing with less code:

    const getWord = () => 'unicorn';
    

    unicorn is returned even though you don't ever explicitly type return anywhere.

    In your first example, you wrapped your JSX in parenthesis. The equivalent in our simple example is:

    const getWord = () => ('unicorn');
    

    or this

    const getWord = () => (
      'unicorn'
    );
    

    The last four examples are equivalent. Hope that helps!

    0 讨论(0)
提交回复
热议问题