0

I'm looking at react's tutorial page, and it shows:

import React, { Component } from 'react';
import { AppRegistry, Text } from 'react-native';

class HelloWorldApp extends Component {
  render() {
    return (
      <Text>Hello world!</Text>
    );
  }
}

AppRegistry.registerComponent('HelloWorldApp', () => HelloWorldApp);
                                             // ^ this part 

What does it means? Is it equal to:

function() {
  return HelloWorldApp
}

If it so, why not using:

HelloWorldApp 

directly?

Kokizzu
  • 24,974
  • 37
  • 137
  • 233
  • 2
    function that returns HelloWorldApp is not same as HelloWorldApp – Bryan Chen Dec 14 '16 at 02:16
  • 1
    The reason you would use a function is to make it lazy loading. That way the class is not actually resolved until you use the component. – max Dec 14 '16 at 02:21

1 Answers1

4

That's an ES6 arrow function. It's a function that doesn't need any arguments and returns HelloWorldApp. It's equivalent to:

AppRegistry.registerComponent('HelloWorldApp', function() {
    return HelloWorldApp;
});

As for why it needs a function and not just the React component, I'm not sure. Perhaps it's just an API decision by React Native.

GJK
  • 37,023
  • 8
  • 55
  • 74