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?
Differences Between Functional and Class-Components
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.
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:
- You end up with less code
- 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
- The React group stated that there might be a performance boost for a functional component in upcoming React versions
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!