How to use functional components for React Native development

In this post, we will learn about How to use functional components for React Native development, as we all know we can develop React Native apps using two types either classes or functions. In the class component, we can able to use state using but in 2019 with the release of React 16.8, React Hooks finally becomes available to develop applications in React Native and now we can use state in functional components using hooks. and it’s a good practice to write react native code using the functional components.

Why Functional component?

  1. The functional component makes it easy to read, write & debug code for developers.
  2. No need to use ‘this’ word, also don’t need to use bind to call methods.
  3. It allows you to use React Hooks which makes state management very easy.
  4. It reduces code and makes smaller bundle sizes.

Now let’s understand with basic syntax difference between classes & functional components.

1. Common syntax for class component

here’s the syntax for the class component. it will display text on the screen

Now, let’s understand the same example with a functional component using react hooks

Now you can see in the above functional example. the number of lines is reduced in code. this is an advantage of the functional component. also, you can use const as follows.

2. How to use state in functional component

first, will see how to use state in the class component

Now, let’s understand the same example with functional component

3. Life cycle for the functional component

React native life cycle for the functional component is very easy & simple. We just need to useEffect hooks to work the same as componentDidMount, componentDidUpdate, and componentWillUnmount methods. useEffect accepts two parameters, first one is callback with runs after render/return data on the screen. second in the optional dependency array which accepts a parameter. after passing the parameter in the array we simply tell the effect only run when that parameter changes. if you want to understand React Native class life cycle the check out this post

This is How we can use functional components for React Native development


Thank you 🙂




Leave a Reply

Your email address will not be published.