React Native

How do you handle asynchronous actions in React Native?

Medium
36
Added
You can handle asynchronous actions in React Native using the useEffect hook or by using libraries like Redux-Saga or Redux-Thunk.

Solution Code

React Native
-- Example Code --
import React, { useState, useEffect } from "react";
import { View, Text } from "react-native";

const App = () => {
  const [data, setData] = useState(null);

  useEffect(() => {
    const fetchData = async () => {
      const response = await fetch("https://api.example.com/data");
      const json = await response.json();
      setData(json);
    };
    fetchData();
  }, []);

  return (
    <View>
      <Text>{data ? data.message : "Loading..."}</Text>
    </View>
  );
};

export default App;
Explanation
This code demonstrates how to handle asynchronous actions using the useEffect hook.

Guided Hints

{'hint': 'Use useEffect for side effects.'}
{'hint': 'Use async/await for asynchronous operations.'}