Lesson 32 of 60 – Functional Components in React Native
53%

Functional Components in React Native

Functional components are JavaScript functions that return React Native UI. They are the most common way to create components in modern React Native applications.

Note: A functional component can return React Native elements such as View, Text, Image, Pressable and other components.

1. What is a Functional Component?

A functional component is a JavaScript function that returns a React element.

function Welcome() {

    return (
        <Text>
            Welcome to React Native
        </Text>
    );

}

Here, Welcome is a functional component.

2. Importing React Native Components

import React from 'react';

import {
    View,
    Text
} from 'react-native';

The React Native components required by the functional component must be imported.

3. Basic Functional Component

function App() {

    return (

        <View>

            <Text>
                Hello React Native
            </Text>

        </View>

    );

}

The component returns a View containing a Text component.

4. Exporting a Functional Component

A component can be exported so it can be imported and used in another file.

export default function App() {

    return (

        <Text>
            Hello
        </Text>

    );

}

5. Rendering a Functional Component

function Greeting() {

    return (
        <Text>
            Welcome
        </Text>
    );

}

export default function App() {

    return (

        <View>

            <Greeting />

        </View>

    );

}

The Greeting component is rendered using <Greeting />.

6. Functional Component with Text

function WelcomeMessage() {

    return (

        <Text>
            Welcome to our mobile application.
        </Text>

    );

}

Functional components can be used to create small reusable UI elements.

7. Functional Component with View

function Profile() {

    return (

        <View>

            <Text>
                Student Profile
            </Text>

            <Text>
                React Native Student
            </Text>

        </View>

    );

}

A functional component can contain multiple React Native components.

8. Functional Component with Styles

import {
    View,
    Text,
    StyleSheet
} from 'react-native';

function Welcome() {

    return (

        <View style={styles.container}>

            <Text style={styles.title}>
                Welcome
            </Text>

        </View>

    );

}

const styles = StyleSheet.create({

    container: {
        padding: 20
    },

    title: {
        fontSize: 24,
        fontWeight: 'bold'
    }

});

9. Functional Components with Props

Props allow a functional component to receive data from its parent.

function Greeting({ name }) {

    return (

        <Text>
            Hello {name}
        </Text>

    );

}

<Greeting name="Rahul" />

10. Multiple Props

function Student({
    name,
    course
}) {

    return (

        <View>

            <Text>
                Name: {name}
            </Text>

            <Text>
                Course: {course}
            </Text>

        </View>

    );

}

<Student
    name="Rahul"
    course="React Native"
/>

A component can receive multiple props.

11. Default Props Values

A default value can be provided using JavaScript default parameters.

function Greeting({
    name = 'Student'
}) {

    return (

        <Text>
            Hello {name}
        </Text>

    );

}

<Greeting />

If no name is provided, the component uses Student.

12. Functional Component with Image

function ProfileImage() {

    return (

        <Image
            source={require('./assets/user.jpg')}
            style={{
                width: 100,
                height: 100,
                borderRadius: 50
            }}
        />

    );

}

A functional component can encapsulate image display and styling.

13. Functional Component with Pressable

function LoginButton() {

    return (

        <Pressable
            onPress={() =>
                console.log('Login')
            }
        >

            <Text>
                Login
            </Text>

        </Pressable>

    );

}

Functional components can contain interactive elements.

14. Functional Components and Events

function SubmitButton() {

    const handlePress = () => {

        console.log(
            'Submitted'
        );

    };

    return (

        <Pressable
            onPress={handlePress}
        >

            <Text>
                Submit
            </Text>

        </Pressable>

    );

}

Event handler functions can be defined inside functional components.

15. Functional Components with State

Functional components can use the useState hook to manage changing data.

import React, {
    useState
} from 'react';

function Counter() {

    const [count, setCount] =
        useState(0);

    return (

        <View>

            <Text>
                Count: {count}
            </Text>

            <Button
                title="Increase"
                onPress={() =>
                    setCount(count + 1)
                }
            />

        </View>

    );

}

16. Functional Components with useEffect

The useEffect hook can perform side effects in a functional component.

import React, {
    useEffect
} from 'react';

function App() {

    useEffect(() => {

        console.log(
            'Component loaded'
        );

    }, []);

    return (

        <Text>
            Home
        </Text>

    );

}

An empty dependency array makes this effect run after the initial render.

17. Conditional Rendering

Functional components can display different UI depending on conditions.

function Status({ loggedIn }) {

    return (

        <View>

            {loggedIn ? (

                <Text>
                    Welcome Back
                </Text>

            ) : (

                <Text>
                    Please Login
                </Text>

            )}

        </View>

    );

}

18. Rendering Lists in Functional Components

function StudentList() {

    const students = [
        'Rahul',
        'Amit',
        'Priya'
    ];

    return (

        <View>

            {students.map(
                student => (

                    <Text
                        key={student}
                    >
                        {student}
                    </Text>

                )
            )}

        </View>

    );

}

Functional components can render dynamic lists using JavaScript array methods.

19. Nested Functional Components

function Header() {

    return (
        <Text>
            My App
        </Text>
    );

}

function Profile() {

    return (
        <Text>
            Student Profile
        </Text>
    );

}

function App() {

    return (

        <View>

            <Header />

            <Profile />

        </View>

    );

}

Multiple functional components can be combined to build a screen.

20. Reusable Card Component

function CourseCard({
    title,
    fee
}) {

    return (

        <View style={styles.card}>

            <Text style={styles.title}>
                {title}
            </Text>

            <Text>
                Fee: {fee}
            </Text>

        </View>

    );

}

The component can be reused for multiple courses.

21. Passing Functions as Props

A parent component can pass a function to a child component as a prop.

function ActionButton({
    onPress
}) {

    return (

        <Pressable
            onPress={onPress}
        >

            <Text>
                Save
            </Text>

        </Pressable>

    );

}

function App() {

    const saveData = () => {

        console.log(
            'Data saved'
        );

    };

    return (

        <ActionButton
            onPress={saveData}
        />

    );

}

22. Children in Functional Components

The children prop allows a component to receive nested content.

function Card({ children }) {

    return (

        <View style={styles.card}>

            {children}

        </View>

    );

}

function App() {

    return (

        <Card>

            <Text>
                Student Details
            </Text>

        </Card>

    );

}

23. Functional Component with Form

function LoginForm() {

    const [email, setEmail] =
        useState('');

    return (

        <View>

            <TextInput
                placeholder="Email"
                value={email}
                onChangeText={setEmail}
            />

            <Button
                title="Login"
                onPress={() =>
                    console.log(email)
                }
            />

        </View>

    );

}

Hooks and functional components are commonly used together to create interactive forms.

24. Functional Component with API Data

function Students() {

    const [students, setStudents] =
        useState([]);

    useEffect(() => {

        fetch('https://example.com/students')
            .then(response =>
                response.json()
            )
            .then(data =>
                setStudents(data)
            );

    }, []);

    return (

        <View>

            <Text>
                Students: {students.length}
            </Text>

        </View>

    );

}

Functional components can fetch and display data using hooks.

25. Component File Structure

Functional components are commonly placed in separate files.

src/
│
├── components/
│   ├── Header.js
│   ├── Button.js
│   ├── CourseCard.js
│   └── ProfileCard.js
│
├── screens/
│   ├── Home.js
│   └── Login.js
│
└── App.js

This organization becomes especially useful as the application grows.

26. Common Functional Component Mistakes

  • Forgetting to return JSX.
  • Using lowercase names for custom components.
  • Forgetting to import React Native components.
  • Using props without passing them correctly.
  • Changing state directly instead of using its setter.
  • Creating unnecessarily large components.
  • Repeating UI instead of creating reusable components.
  • Forgetting keys when rendering lists.
  • Putting unrelated responsibilities into one component.

27. Complete Functional Component Example

import React, {
    useState
} from 'react';

import {
    View,
    Text,
    Pressable,
    StyleSheet
} from 'react-native';

function CourseCard({
    title,
    duration,
    fee
}) {

    const [started, setStarted] =
        useState(false);

    return (

        <View style={styles.card}>

            <Text style={styles.title}>
                {title}
            </Text>

            <Text>
                Duration: {duration}
            </Text>

            <Text style={styles.fee}>
                Fee: {fee}
            </Text>

            <Pressable
                style={styles.button}
                onPress={() =>
                    setStarted(true)
                }
            >

                <Text style={styles.buttonText}>

                    {started
                        ? 'Course Started'
                        : 'Start Course'}

                </Text>

            </Pressable>

        </View>

    );

}

export default function App() {

    return (

        <View style={styles.container}>

            <Text style={styles.heading}>
                Available Courses
            </Text>

            <CourseCard
                title="React Native"
                duration="6 Months"
                fee="₹15000"
            />

            <CourseCard
                title="JavaScript"
                duration="4 Months"
                fee="₹8000"
            />

        </View>

    );

}

const styles = StyleSheet.create({

    container: {
        flex: 1,
        padding: 20,
        gap: 15
    },

    heading: {
        fontSize: 26,
        fontWeight: 'bold',
        marginBottom: 10
    },

    card: {
        padding: 20,
        backgroundColor: 'white',
        borderRadius: 12,
        borderWidth: 1,
        borderColor: '#E2E8F0'
    },

    title: {
        fontSize: 21,
        fontWeight: 'bold',
        marginBottom: 8
    },

    fee: {
        fontSize: 18,
        fontWeight: 'bold',
        marginTop: 8
    },

    button: {
        marginTop: 15,
        padding: 14,
        borderRadius: 8,
        backgroundColor: '#2563EB',
        alignItems: 'center'
    },

    buttonText: {
        color: 'white',
        fontWeight: 'bold'
    }

});

This example demonstrates a reusable functional component using props and state.

28. Functional Component Best Practices

  • Use meaningful component names.
  • Keep each component focused on a clear responsibility.
  • Use props for reusable data.
  • Use state only when data needs to change.
  • Use hooks when component logic requires them.
  • Create reusable components for repeated UI.
  • Keep styles organized.
  • Separate large screens into smaller components.
  • Keep component files readable.
  • Use a clear folder structure.

29. Functional vs Class Components

Functional Component Class Component
JavaScript function JavaScript class
Can use React Hooks Uses class state and lifecycle methods
Generally simpler to write Requires more class syntax
Common approach in modern React Native development Still found in some existing codebases

30. Complete Functional Component Workflow

Identify UI Requirement
        ↓
Create Functional Component
        ↓
Import Required Components
        ↓
Return JSX
        ↓
Add Styles
        ↓
Receive Props if Needed
        ↓
Add State if Needed
        ↓
Handle Events
        ↓
Reuse Component
        ↓
Combine Components into Screens

Functional components provide a simple and flexible way to build React Native applications. By combining components, props, state, hooks and events, you can create reusable UI and complete mobile application screens.

📌 Key Points

  • A functional component is a JavaScript function that returns UI.
  • Functional components can contain React Native components.
  • Props allow data to be passed into components.
  • Hooks such as useState can manage changing data.
  • useEffect can handle side effects.
  • Functional components can respond to user events.
  • Components can be combined to create complete screens.
  • Reusable components reduce duplicate code.
  • Components can receive functions and children as props.
  • Functional components are widely used in modern React Native applications.

🧠 Quick Quiz

Question: What does a functional component normally return in React Native?