<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Karim Mohammadi</title>
    <description>The latest articles on DEV Community by Karim Mohammadi (@skmohammadi).</description>
    <link>https://dev.to/skmohammadi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F782106%2F85e444b7-e279-45fd-9dd8-e54f31603d9d.png</url>
      <title>DEV Community: Karim Mohammadi</title>
      <link>https://dev.to/skmohammadi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/skmohammadi"/>
    <language>en</language>
    <item>
      <title>How to Build a Global Refresh System in React Native</title>
      <dc:creator>Karim Mohammadi</dc:creator>
      <pubDate>Fri, 26 Dec 2025 11:16:59 +0000</pubDate>
      <link>https://dev.to/skmohammadi/how-to-build-a-global-refresh-system-in-react-native-3j4h</link>
      <guid>https://dev.to/skmohammadi/how-to-build-a-global-refresh-system-in-react-native-3j4h</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Do you want to refresh many components at once in React Native? This guide shows you how. We will build a simple refresh system. When a user pulls down the screen, all data updates together.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Imagine your app has many parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A user profile section&lt;/li&gt;
&lt;li&gt;A news feed section&lt;/li&gt;
&lt;li&gt;A notifications section&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each part has its own data. When the user pulls to refresh, you want ALL parts to update. How do you do this?&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution: Refresh Context
&lt;/h2&gt;

&lt;p&gt;We use React Context. Context lets components share data. Our context will share refresh functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Create the Refresh Context
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useCallback, useState } from "react";

export const useRefreshControls = () =&amp;gt; {
  const [isRefreshing, setIsRefreshing] = useState(false);
  const refreshFunctions = React.useRef(new Map());

  const registerRefreshHandler = useCallback((id, refreshFn) =&amp;gt; {
    refreshFunctions.current.set(id, refreshFn);
  }, []);

  const unregisterRefreshHandler = useCallback((id) =&amp;gt; {
    refreshFunctions.current.delete(id);
  }, []);

  const triggerRefresh = useCallback(async () =&amp;gt; {
    setIsRefreshing(true);
    try {
      const promises = Array.from(refreshFunctions.current.values())
        .map((fn) =&amp;gt; fn());
      await Promise.all(promises);
    } finally {
      setIsRefreshing(false);
    }
  }, []);

  return {
    isRefreshing,
    triggerRefresh,
    registerRefreshHandler,
    unregisterRefreshHandler,
  };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Create a Subscribe Hook
&lt;/h2&gt;

&lt;p&gt;This hook lets components register their refresh function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const useSubscribeToRefresh = (id, refreshFn) =&amp;gt; {
  const { registerRefreshHandler, unregisterRefreshHandler } = useRefreshContext();

  React.useEffect(() =&amp;gt; {
    registerRefreshHandler(id, refreshFn);
    return () =&amp;gt; unregisterRefreshHandler(id);
  }, [id, refreshFn]);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Set Up the Provider
&lt;/h2&gt;

&lt;p&gt;Wrap your app with the provider.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  const refreshControls = useRefreshControls();

  return (
    &amp;lt;RefreshProvider value={refreshControls}&amp;gt;
      &amp;lt;ScrollView
        refreshControl={
          &amp;lt;RefreshControl
            refreshing={refreshControls.isRefreshing}
            onRefresh={refreshControls.triggerRefresh}
          /&amp;gt;
        }
      &amp;gt;
        &amp;lt;UserProfile /&amp;gt;
        &amp;lt;NewsFeed /&amp;gt;
      &amp;lt;/ScrollView&amp;gt;
    &amp;lt;/RefreshProvider&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Use in Components
&lt;/h2&gt;

&lt;p&gt;Each component registers its own refresh function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function UserProfile() {
  const [user, setUser] = useState(null);

  const fetchUser = async () =&amp;gt; {
    const data = await api.getUser();
    setUser(data);
  };

  useSubscribeToRefresh("user-profile", fetchUser);

  return &amp;lt;Text&amp;gt;{user?.name}&amp;lt;/Text&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function NewsFeed() {
  const [posts, setPosts] = useState([]);

  const fetchPosts = async () =&amp;gt; {
    const data = await api.getPosts();
    setPosts(data);
  };

  useSubscribeToRefresh("news-feed", fetchPosts);

  return posts.map((post) =&amp;gt; &amp;lt;PostCard key={post.id} post={post} /&amp;gt;);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How It Works
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;User pulls down the screen&lt;/li&gt;
&lt;li&gt;triggerRefresh runs&lt;/li&gt;
&lt;li&gt;All registered functions run at the same time&lt;/li&gt;
&lt;li&gt;Spinner shows while loading&lt;/li&gt;
&lt;li&gt;Spinner hides when all data is ready&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Benefits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Simple: Easy to add to any component&lt;/li&gt;
&lt;li&gt;Fast: All requests run in parallel&lt;/li&gt;
&lt;li&gt;Clean: Components manage their own data&lt;/li&gt;
&lt;li&gt;Automatic: Cleanup happens when components unmount&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This pattern makes pull-to-refresh easy in React Native. Each component registers its refresh function. One pull updates everything. Your users get fresh data fast.&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactnative</category>
      <category>ui</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
