Managing State and Lifecycle in Mobile Apps

Tutorial 3 of 5

Introduction

Welcome to this tutorial on managing state and lifecycle in React Native apps. The goal of this tutorial is to help you understand how to manage data within a component using State and control a component's lifecycle in React Native, the popular JavaScript library for building mobile apps.

By the end of this tutorial, you will learn:
- What state and lifecycle are in React Native
- How to manage state and lifecycle in your components

Before starting, you should have a basic understanding of JavaScript and React Native. Knowing how to create a basic React Native app will be helpful but not necessary as we will cover all the basics.

Step-by-Step Guide

What is State?

In React Native, "state" is an object that holds data that may change over the component's lifecycle. State is private and fully controlled by the component. For example, in a Todo list app, the list of todos can be a state.

What is Lifecycle?

Component lifecycle is a series of methods that get automatically called during the life of a component. These methods can be used to perform actions before or after a component renders, updates, or unmounts.

Managing State

To create a state in a component, we use the useState hook:

const [state, setState] = useState(initialState);

state is the current state, setState is a function that updates the state, and initialState is the initial state.

Managing Lifecycle

React Native components have several lifecycle methods, but the most commonly used ones are componentDidMount, componentDidUpdate, and componentWillUnmount.

useEffect(() => {
  // componentDidMount equivalent
  return () => {
    // componentWillUnmount equivalent
  };
}, []);

useEffect(() => {
  // componentDidUpdate equivalent
});

Code Examples

Let's create a simple counter app.

import React, { useState, useEffect } from 'react';
import { Button, Text, View } from 'react-native';

const Counter = () => {
  // Declare a new state variable called "count"
  const [count, setCount] = useState(0);

  useEffect(() => {
    // Update the document title
    if (count > 0) {
      console.log(`You clicked ${count} times`);
    }
  });

  return (
    <View>
      <Text>You clicked {count} times</Text>
      <Button onPress={() => setCount(count + 1)} title="Click me" />
    </View>
  );
};

export default Counter;

Here's how it works:
- When the Counter function is called, it returns a View that displays the current state (count) and a Button that can change the state.
- When the button is pressed, the onPress handler is called, which calls the setCount function with the new count.
- This triggers a re-render, and the Text inside the View displays the new count.
- The useEffect hook is then called, logging the new count to the console.

Summary

In this tutorial, we've learned about two key concepts in React Native: state and lifecycle. We've seen how to use the useState hook to manage state and the useEffect hook to manage lifecycle.

Next steps would be to explore other hooks in React Native and how to manage state in more complex apps using tools like Redux or MobX.

Practice Exercises

  1. Create a simple timer app: display a timer that increments every second and a button to reset the timer.

  2. Modify the timer app to stop incrementing after reaching 60 seconds.

  3. Create an app that fetches and displays a random joke from an API every time a button is clicked. Use the Official Joke API.

Note: Always remember that practice is the key to mastering any programming concept. So, keep coding and exploring different ways to manage state and lifecycle in React Native. Happy coding!