<?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: Ayomide Daniel</title>
    <description>The latest articles on DEV Community by Ayomide Daniel (@theaydev).</description>
    <link>https://dev.to/theaydev</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%2F779749%2Fbc32eb84-d112-439e-88d3-b0b9f0ccff15.jpg</url>
      <title>DEV Community: Ayomide Daniel</title>
      <link>https://dev.to/theaydev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/theaydev"/>
    <language>en</language>
    <item>
      <title>Build React Native Apps: Offline-First Approach Using GraphQL and Caching</title>
      <dc:creator>Ayomide Daniel</dc:creator>
      <pubDate>Wed, 14 May 2025 14:05:43 +0000</pubDate>
      <link>https://dev.to/theaydev/build-react-native-apps-offline-first-approach-using-graphql-and-caching-33gn</link>
      <guid>https://dev.to/theaydev/build-react-native-apps-offline-first-approach-using-graphql-and-caching-33gn</guid>
      <description>&lt;p&gt;Building apps that work offline isn’t just a nice-to-have anymore, it’s essential. Network issues are real, especially in places with unstable connections. Whether it's users in low-connectivity areas or those who just want snappy experiences, handling offline gracefully is one of the most impactful upgrades you can bring to your mobile app.&lt;/p&gt;

&lt;p&gt;In this guide, we'll walk through how to build an offline-first experience in a React Native app using Expo, GraphQL (via Apollo Client), and efficient local caching strategies that help your app shine even when the connection doesn’t.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Offline-First Matters
&lt;/h2&gt;

&lt;p&gt;Users expect apps to "just work," even when the internet doesn’t. An offline-first approach ensures that your app can not only read data when offline but also queue mutations while offline and sync when back online.&lt;/p&gt;

&lt;p&gt;Here’s why investing in offline support is a great move:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Better UX: Users can interact with the app even with poor/no internet&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Faster perceived performance: Cached data loads instantly&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Increased reliability: Your app doesn’t freeze or crash when connectivity drops&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Wider market reach: Works better in emerging markets with spotty networks&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;User trust: Offline access builds confidence that your app "just works."&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Apps that function well offline are often described by users as “smooth,” even if they don’t consciously recognize what’s different.&lt;/p&gt;

&lt;p&gt;Apollo Client makes this possible out of the box with its normalized cache and retry link system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tech Stack Overview
&lt;/h2&gt;

&lt;p&gt;To make this all work smoothly, we’re combining the following tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;React Native with Expo: Fast setup, consistent development experience&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Apollo Client: Handling GraphQL queries and mutations&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;@react-native-async-storage/async-storage: To store persistent cache data&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;expo-network: For detecting network availability&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This combination keeps things flexible, lightweight, and scalable.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Set Up Apollo Client with Cache Persistence
&lt;/h2&gt;

&lt;p&gt;First, let’s install the packages we’ll need::&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;expo install @react-native-async-storage/async-storage expo-network
npm install @apollo/client graphql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now create a helper to initialize Apollo with cache persistence:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// lib/apollo.ts
import {
  ApolloClient,
  InMemoryCache,
  createHttpLink,
  ApolloProvider,
} from '@apollo/client';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { persistCache } from 'apollo3-cache-persist';
import { useEffect, useState } from 'react';

const cache = new InMemoryCache();
const link = createHttpLink({ uri: 'https://your-graphql-api.com/graphql' });

export const useApollo = () =&amp;gt; {
  const [client, setClient] = useState&amp;lt;ApolloClient&amp;lt;any&amp;gt; | null&amp;gt;(null);

  useEffect(() =&amp;gt; {
    (async () =&amp;gt; {
      await persistCache({
        cache,
        storage: AsyncStorage,
      });
      const apolloClient = new ApolloClient({
        link,
        cache,
        connectToDevTools: true,
      });
      setClient(apolloClient);
    })();
  }, []);

  return client;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then use this client to wrap your app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// App.tsx
import { ApolloProvider } from '@apollo/client';
import { useApollo } from './lib/apollo';
import { View, Text } from 'react-native';

export default function App() {
  const client = useApollo();

  if (!client) return &amp;lt;Text&amp;gt;Loading Apollo...&amp;lt;/Text&amp;gt;;

  return (
    &amp;lt;ApolloProvider client={client}&amp;gt;
      {/* Your app components here */}
    &amp;lt;/ApolloProvider&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This setup ensures Apollo’s in-memory cache gets persisted between app sessions using AsyncStorage.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Detect Network Status with expo-network
&lt;/h2&gt;

&lt;p&gt;Knowing whether the user is online or offline lets you switch behavior dynamically. Use this to switch between online and offline logic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as Network from 'expo-network';

const isOnline = async () =&amp;gt; {
  const status = await Network.getNetworkStateAsync();
  return status.isConnected &amp;amp;&amp;amp; status.isInternetReachable;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use this to conditionally show offline banners, queue or defer mutations, disable form submissions if offline. Even simple checks like these can dramatically improve perceived quality.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Reading and Writing from the Cache
&lt;/h2&gt;

&lt;p&gt;When offline, direct cache access helps keep things smooth. Apollo Client gives you methods like readQuery and writeQuery for this purpose.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const cachedData = client.readQuery({ query: GET_USER });
client.writeQuery({ query: GET_USER, data: newUserData });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows components to pull and push to the cache without requiring a network trip. Great for implementing things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Offline profile editing&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Caching form drafts&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Preloading views from previous sessions&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To go even further, you can set up a local mutation queue and retry logic that flushes once the device is back online.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. UX Considerations
&lt;/h2&gt;

&lt;p&gt;Going offline-first isn’t just technical, it’s also about user experience. Consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Skeleton loaders when fetching (even cached) data&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Retry buttons when fetch or save fails&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Visual indicators like a toast or banner when offline&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Degraded but useful experiences, not blank screens&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Manual refresh to revalidate stale cache&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If users can’t notice when they’ve gone offline, you’ve done something right&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Tips
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Apollo’s in-memory cache is volatile unless you persist it manually.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Default fetch policies like cache-first or cache-and-network can drastically reduce re-renders&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Always assume mutations will fail when offline. Don’t rely on automatic retries&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Logging cache writes during development helps debug data flow&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Not every query needs persistence. Be selective&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Avoid invalidating cache too aggressively. Let it breathe&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Making your React Native app offline-ready using Apollo Client and caching techniques isn’t just about edge cases, it’s about delivering a consistently smooth user experience. With a bit of setup, you can enable your app to handle flaky networks with grace.&lt;/p&gt;

&lt;p&gt;Focus on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Persisting meaningful cache to avoid unnecessary re-fetches&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Detecting network status reliably&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Building clean UX patterns around connectivity changes&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Apps that work offline feel faster, more stable, and more thoughtful. Users love them and they keep coming back&lt;/p&gt;

&lt;p&gt;Want to see all of this in action? Check out the &lt;a href="https://github.com/ayomidedaniel1/expo-offline-graphql" rel="noopener noreferrer"&gt;full source code&lt;/a&gt; on GitHub&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>graphql</category>
      <category>mobile</category>
      <category>developers</category>
    </item>
    <item>
      <title>Mastering Git Commit Messages: A Guide to Effective Version Control</title>
      <dc:creator>Ayomide Daniel</dc:creator>
      <pubDate>Fri, 21 Mar 2025 22:27:22 +0000</pubDate>
      <link>https://dev.to/theaydev/mastering-git-commit-messages-a-guide-to-effective-version-control-hgo</link>
      <guid>https://dev.to/theaydev/mastering-git-commit-messages-a-guide-to-effective-version-control-hgo</guid>
      <description>&lt;p&gt;Ever opened a Git history and felt like deciphering ancient &lt;em&gt;hieroglyphs&lt;/em&gt;? Yeah, we've all been there. Let's face it, most Git logs resemble cryptic ancient texts, leaving fellow developers deciphering emojis and obscure acronyms. But fear not! Crafting clear and informative commit messages is easier than you think, and it can transform your Git history from a messy scroll into a compelling chronicle of your coding prowess.&lt;/p&gt;

&lt;p&gt;Git commit messages are more than just annotations to your code changes; they are the narrative of your project's history. A well-crafted commit message not only documents what changes were made but also why they were made. Commit messages play a crucial role in keeping your Git history clean, organized, and understandable. By adopting a consistent style, you enhance collaboration and ease the process of code maintenance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Care About Commit Messages?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Clarity and Understanding:&lt;/strong&gt;&lt;br&gt;
Clear commit messages help you and your team understand the purpose and context of each change. This is especially valuable when revisiting code after some time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Collaboration:&lt;/strong&gt;&lt;br&gt;
When working with a team, descriptive commit messages facilitate effective collaboration. Team members can quickly grasp the intention behind changes and make informed decisions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Troubleshooting:&lt;/strong&gt;&lt;br&gt;
Well-documented commits ease the process of troubleshooting and debugging. If an issue arises, a detailed Git history can help pinpoint when and why a particular change was introduced.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Best Practices for Writing Commit Messages
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use the imperative mood:&lt;/strong&gt;&lt;br&gt;
Instead of "Fixed login issue," write "Fix login issue."&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Keep it concise:&lt;/strong&gt;&lt;br&gt;
Summarize the change in 50 characters or less for the first line.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Provide context when needed:&lt;/strong&gt;&lt;br&gt;
Explain why a change was made, not just what was changed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Separate concerns:&lt;/strong&gt;&lt;br&gt;
Each commit should represent a single logical change.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Follow a consistent format:&lt;/strong&gt;&lt;br&gt;
Many teams adopt commit message conventions like Conventional Commits (e.g., feat: add user authentication).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Example of a commit message&lt;/strong&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Good example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fix(auth): resolve issue with token expiration

Tokens were expiring prematurely due to incorrect time conversion.
Updated the logic to ensure proper handling of expiration timestamps.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fix(auth): resolve issue with token expiration
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second commit message is simpler yet gives a concise message of what the commit entails.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bad example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fixed bug
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Here are some conventional commit examples:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;feat: add user authentication&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;fix: resolve login form validation issue&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;perf: optimize image loading&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;refactor: simplify authentication logic&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;docs: update README with setup instructions&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;build: bump react-query from 4.0 to 4.5&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;chore: clean up unused dependencies&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;release: v1.2.0&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Clear and consistent commit messages improve collaboration, debugging, and project maintainability. By following structured guidelines and best practices, you ensure that your Git history remains a useful resource rather than an indecipherable mess. So next time you commit code, take a moment to craft a message that your future self (and your teammates) will thank you for!&lt;/p&gt;

&lt;p&gt;What commit message conventions do you follow? Drop your thoughts in the comments!&lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>React Deep Dive: Mastering useRef for Enhanced Control and Performance</title>
      <dc:creator>Ayomide Daniel</dc:creator>
      <pubDate>Fri, 21 Mar 2025 21:45:52 +0000</pubDate>
      <link>https://dev.to/theaydev/react-deep-dive-mastering-useref-for-enhanced-control-and-performance-24g</link>
      <guid>https://dev.to/theaydev/react-deep-dive-mastering-useref-for-enhanced-control-and-performance-24g</guid>
      <description>&lt;h2&gt;
  
  
  Unlocking React's Hidden Power with useRef
&lt;/h2&gt;

&lt;p&gt;While state and props drive most React interactions, &lt;em&gt;useRef&lt;/em&gt; stands out as a powerfool tool that facilitates direct access to DOM elements and persistence of mutable values across renders. This versatile hook can streamline development and optimize performance in various scenarios, enhancing the developer's toolkit.&lt;/p&gt;

&lt;p&gt;Here's a comprehensive guide to using &lt;em&gt;useRef&lt;/em&gt; in React with TypeScript:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Accessing DOM Elements:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Focusing inputs:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3omgcit1lins3tpv8qlg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3omgcit1lins3tpv8qlg.png" alt="Image description" width="800" height="491"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, &lt;em&gt;useRef&lt;/em&gt; is utilized to create a reference (inputRef) to an HTMLInputElement. The useEffect hook ensures that when the component mounts, it focuses on the input element, making it ready for user input.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Measuring elements:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fab5039a58y2xgeu9csk0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fab5039a58y2xgeu9csk0.png" alt="Image description" width="800" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, &lt;em&gt;useRef&lt;/em&gt; is employed to create a reference (containerRef) to a div element. The useEffect hook logs the bounding rectangle of the referenced element when the component mounts. This can be useful for obtaining position and size information.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Triggering animations:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F00msbs3xil8rwnrvuiph.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F00msbs3xil8rwnrvuiph.png" alt="Image description" width="800" height="676"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, &lt;em&gt;useRef&lt;/em&gt; is used to create a reference (elementRef) to a div element. The handleClick function triggers an animation by adding the 'animated' class to the referenced div element when a button is clicked.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Storing Mutable Values:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Persisting data across renders:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9gikpyh3cpymdao4qdh2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9gikpyh3cpymdao4qdh2.png" alt="Image description" width="800" height="769"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, &lt;em&gt;useRef&lt;/em&gt; is employed to persistently store the value returned by setInterval, creating a reference named intervalRef. The useEffect hook ensures that the interval is set up when the component mounts and cleared when the component unmounts, preventing memory leaks.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Advanced Use Cases:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Creating custom hooks with refs:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftdzly08nha97zlrhgpsu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftdzly08nha97zlrhgpsu.png" alt="Image description" width="800" height="530"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This snippet showcases how &lt;em&gt;useRef&lt;/em&gt; can be used to create custom hooks, such as useClickOutside. This hook takes a reference to an HTML element (ref) and includes useEffect logic to handle clicks outside the specified element. It's a powerful abstraction for common patterns in React applications.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Optimizing performance with refs:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnfsyr7tq8t9kmrjj6ll6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnfsyr7tq8t9kmrjj6ll6.png" alt="Image description" width="800" height="862"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, &lt;em&gt;useRef&lt;/em&gt; is utilized to optimize performance by caching expensive data. The useEffect hook ensures that the expensive data is fetched only once when the component mounts. If the data has already been fetched, the ref allows you to skip unnecessary fetch operations on subsequent renders. This pattern is particularly valuable for optimizing resource-intensive operations in your React components.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;useRef&lt;/em&gt; unlocks a treasure trove of possibilities for React developers. It grants you direct access to the DOM, letting you tweak and manipulate elements with laser-like precision. This opens doors for fine-grained control beyond what state alone can offer. Plus, skipping unnecessary re-renders leads to performance boosts, making your apps run smooth and silky.&lt;/p&gt;

&lt;p&gt;But the magic doesn't stop there. useRef empowers you to craft custom hooks and tackle advanced optimization strategies. This separation of state and DOM interactions keeps your code clean and maintainable, a win for both you and your teammates. So, don't wait – embrace useRef and take your React skills to the next level!&lt;/p&gt;

&lt;p&gt;Embracing the power of useRef empowers React developers to elevate their development practices, fostering more efficient, maintainable, and performant applications. &lt;/p&gt;

&lt;p&gt;Start incorporating useRef into your React arsenal and witness the positive impact on your development workflow and application outcomes!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>typescript</category>
    </item>
    <item>
      <title>A guide to env variables in react native</title>
      <dc:creator>Ayomide Daniel</dc:creator>
      <pubDate>Fri, 21 Mar 2025 21:35:40 +0000</pubDate>
      <link>https://dev.to/theaydev/a-guide-to-env-variables-in-react-native-2npj</link>
      <guid>https://dev.to/theaydev/a-guide-to-env-variables-in-react-native-2npj</guid>
      <description>&lt;p&gt;React Native, the powerful framework for building cross-platform apps, thrives on code reusability and flexibility. But managing sensitive data like API keys, development URLs, or feature flags directly in your codebase can be risky and impractical. Here's where environment variables (env variables) come in, offering a secure and dynamic approach to configuration management.&lt;/p&gt;

&lt;p&gt;While familiar to frontend devs, using environment variables in React Native has some key differences that can trip up newcomers. &lt;/p&gt;

&lt;p&gt;Think you can just use &lt;em&gt;process.env&lt;/em&gt; in React Native like the good old days of frontend development? Hold your horses! While &lt;em&gt;process.env&lt;/em&gt; exists in Node.js, React Native runs your JavaScript code in the Hermes engine, which lacks this module.  But before you panic, there's good news!&lt;/p&gt;

&lt;p&gt;React Native offers specific solutions for managing environment variables, tailored to its cross-platform needs&lt;/p&gt;

&lt;p&gt;There are two main approaches to accessing env variables in React Native:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using Libraries:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/goatandsheep/react-native-dotenv" rel="noopener noreferrer"&gt;react-native-dotenv&lt;/a&gt;: This popular library automatically loads env variables from a .env file at runtime.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/lugg/react-native-config" rel="noopener noreferrer"&gt;react-native-config&lt;/a&gt;: This library offers more advanced features like custom parsing and handling platform-specific variables.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Using Native Modules:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For more granular control and access to native platform features, you can create custom native modules to retrieve env variables directly from the device's environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;react-native-dotenv&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Installation:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -D react-native-dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Setup:&lt;/strong&gt; babel.config.js
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;api.cache(false)
module.exports = {
  plugins: [
    ['module:react-native-dotenv']
  ]
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is our .env file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;API_TOKEN=abc123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and it can be accessed in our code like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {API_TOKEN} from "@env"

console.log(API_TOKEN)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;react-native-config&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;As said above, the specificity of this library is that it allows you to access your variables on both sides. They did that by parsing the .env files directly on the native sides and generating a map.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Config from "react-native-config";

const token = Config.API_TOKEN; // abc123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;While traditional &lt;em&gt;process.env&lt;/em&gt; might not be your immediate companion in React Native, powerful alternatives like react-native-dotenv and react-native-config effectively manage your env variables. By choosing the right tool for your needs, you'll not only ensure secure and flexible configuration, but also contribute to a cleaner and more maintainable codebase. Remember, these env variable heroes exist to empower your cross-platform React Native development, allowing you to focus on building amazing apps!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Takeaways:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React Native offers tailored solutions for env variables, distinct from process.env.&lt;/li&gt;
&lt;li&gt;react-native-dotenv excels in simplicity and quick integration.&lt;/li&gt;
&lt;li&gt;react-native-config unlocks advanced features and customization.&lt;/li&gt;
&lt;li&gt;Secure your data, choose wisely, and build with confidence!&lt;/li&gt;
&lt;li&gt;Go forth and conquer the env variable landscape in React Native!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>reactnative</category>
      <category>typescript</category>
      <category>javascript</category>
      <category>mobile</category>
    </item>
  </channel>
</rss>
