Functional Components in React Native

Last Updated on August 31, 2020.

Most of the react native developers are shifting from class-based components to functional components. You can even see changes in the examples given in the official documentation of react native. Many of them are demonstrated using functional components.

In this react native tutorial, I am going to show you the basics of functional component with some examples.

Simple Functional Component Example

Following is a simple functional component which has a Text component. It’s the basic syntax of function components in react native.

import React from 'react';
import {Text} from 'react-native';

const App = () => {
  return <Text>Hello World</Text>;
};

export default App;

Functional Component with Props Example

We know props are used to transfer information from one component to another. They are very useful while creating reusable components. Following is a function component named Header with text and a prop named title.

import React from 'react';
import {Text} from 'react-native';

const Header = ({title}) => {
  return <Text>{title}</Text>;
};

export default Header;

Now, you can reuse the Header component as given below.

import React from 'react';
import Header from './Header';

const App = () => {
  return <Header title="Hello World" />;
};

export default App;

Stateful Functional Component Example

Earlier, it was impossible to use State in functional components. Please note that there is no constructer inside a functional component. After the introduction of react hooks, adding state within a functional component is easy. Following is a functional component with TextInput that uses State with the help of useState hook.

import React, {useState} from 'react';
import {TextInput} from 'react-native';

const App = () => {
  const [input, setInput] = useState('');
  return (
    <TextInput
      style={{height: 40, borderColor: 'gray', borderWidth: 1}}
      onChangeText={(text) => setInput(text)}
      value={input}
    />
  );
};

export default App;

Life Cycles in Functional Component Example

You can use useEffect hook to integrate life cycles inside a functional component.

ComponentDidMount in Functional Component

import React, {useState, useEffect} from 'react';
import {TextInput} from 'react-native';

const App = () => {
  const [input, setInput] = useState('');
  const [placeholder, setPlaceholder] = useState('');

  //ComponentDidMount
  useEffect(() => setPlaceholder('username'), []);

  return (
    <TextInput
      style={{height: 40, borderColor: 'gray', borderWidth: 1}}
      placeholder={placeholder}
      onChangeText={(text) => setInput(text)}
      value={input}
    />
  );
};

export default App;

In the above example, the value of the placeholder is changed to username after mounting. Always remember to leave the second argument of useEffect as empty (i.e. [ ]), otherwise useEffect will be called every time when the state changes.

ComponentDidUpdate in Functional Component

import React, {useState, useEffect} from 'react';
import {TextInput} from 'react-native';

const App = () => {
  const [input, setInput] = useState('');

  //ComponentDidUpdate
  useEffect(() => console.log(input), [input]);

  return (
    <TextInput
      style={{height: 40, borderColor: 'gray', borderWidth: 1}}
      onChangeText={(text) => setInput(text)}
      value={input}
    />
  );
};

export default App;

What happens in the above example is the useEffect is called everytime when the input value is changed.

I have covered the basics of react native function components and I hope this react native tutorial will help you.

Leave a Reply

%d bloggers like this: