Want create site? Find Free WordPress Themes and plugins.

After installing and creating a new project of React-Native-App as demonstrated in React Native: Getting Started and creating a React Native: Hello World app here is a further guide  a difference between functional and class components in React and which one is the right choice for you? 

The simplest approach to define a component in React is to write JavaScript functional components:

react native-components

Differences Between Functional and Class-Components

State

Since a functional component is only a plain JavaScript function, you can’t use setState () in your component. That is the reason why they are also known as functional stateless components. So, every time you see a functional component you can be certain that this specific component doesn’t have its state. 

If you require a state in your component you either have to create a class component or you have to lift the state to the parent component and pass it down the functional component via props. 

Lifecycle Hooks

Another element that you can’t use in functional components is lifecycle hooks. The cause is the same as for state, all lifecycle hooks are coming from the React. A component that you drag out from in-class components. 

So, if you require lifecycle hooks you should possibly use a class component. 

Then why should I use functional components at all?

You might inquire why you should use functional components at all if they eliminate so many good features. But there are some advantages you get by using functional components in React:

  1. Functional components are much simpler and easier to read and test because they are plain JavaScript functions without state or lifecycle-hooks
  2. You end up with less code
  3. They assist you to use best practices. It will get simpler to split up the container and presentational components because you have to think more about your component’s state if you don’t have access to setState() in your component
  4. The React group stated that there might be a performance boost for a functional component in upcoming React versions

Conclusion

Thus, to address our inquiry before, you should use functional components if you are writing a presentational component that doesn’t have its very own state or requires to access a lifecycle hook. Else you can stick to class components or check out the library recompose which enables you to write functional components and boost them with a state or lifecycle hooks with HOCs! 

Did you find apk for android? You can find new Free Android Games and apps.