<?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: Ezea Victor</title>
    <description>The latest articles on DEV Community by Ezea Victor (@victorezea).</description>
    <link>https://dev.to/victorezea</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%2F396961%2F698a9de7-0ccd-4fef-b89b-003fff9ed134.jpeg</url>
      <title>DEV Community: Ezea Victor</title>
      <link>https://dev.to/victorezea</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/victorezea"/>
    <language>en</language>
    <item>
      <title>How to Build Custom Modal in React Native</title>
      <dc:creator>Ezea Victor</dc:creator>
      <pubDate>Thu, 12 Jan 2023 16:43:06 +0000</pubDate>
      <link>https://dev.to/victorezea/how-to-build-custom-modal-in-react-native-ghk</link>
      <guid>https://dev.to/victorezea/how-to-build-custom-modal-in-react-native-ghk</guid>
      <description>&lt;p&gt;Animation has become an integral part of mobile development, thanks to React native for its Animated API. In this, we will be building a custom modal with the help of react native animated API using just a button click.&lt;/p&gt;

&lt;p&gt;A Modal component is a basic way to present content above an enclosing view.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisite&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Node &amp;gt;= 16.0.0&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Expo CLI&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To get started with the custom modal we need to generate a new expo project with the following command line.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx create-expo-app -t expo-template-blank-typescript&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;cd into the project and run:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;yarn install @expo/vector-icons&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;yarn start or npm start&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Screen Layout&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the project, directory create a screen folder and a &lt;code&gt;tsx&lt;/code&gt; file with the name &lt;code&gt;src/Screens/ModalScreen.tsx&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";
import { View, StyleSheet, Image, Text, TouchableOpacity } from "react-native";

const ModalScreen = () =&amp;gt; {
  return (
    &amp;lt;View&amp;gt;
      &amp;lt;Text&amp;gt; Modal Screen&amp;lt;/Text&amp;gt;
    &amp;lt;/View&amp;gt;
  );
};

export default ModalScreen;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also need to create the modal component, inside the project directory create the component folder &lt;code&gt;Components/Modals/ModalLayout.tsx&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { View, Text, StyleSheet, Modal, Animated } from "react-native";
import React from "react";

const ModalLayout = () =&amp;gt; {
  return (
    &amp;lt;View&amp;gt;
      &amp;lt;Text&amp;gt;Modal Layout&amp;lt;/Text&amp;gt;
    &amp;lt;/View&amp;gt;
  );
};

export default ModalLayout;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the &lt;code&gt;ModalLayout.tsx&lt;/code&gt; we need to import react hooks &lt;code&gt;useEffect, useState, useRef&lt;/code&gt; to manage our states and also the &lt;code&gt;Animated&lt;/code&gt; API together with &lt;code&gt;Modal&lt;/code&gt; component from react-native.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Animated&lt;/code&gt; library is designed to make animations fluid, powerful, and painless to build and maintain. &lt;code&gt;Animated&lt;/code&gt; focuses on declarative relationships between inputs and outputs, configurable transforms in between, and &lt;code&gt;start&lt;/code&gt;/&lt;code&gt;stop&lt;/code&gt; methods to control time-based animation execution.&lt;/p&gt;

&lt;p&gt;To calculate the scale value of our animation, we will use &lt;code&gt;Animated&lt;/code&gt; value type &lt;code&gt;Animated.Value()&lt;/code&gt; for single values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const scaleValue = useRef(new Animated.Value(0)).current;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A function toggleModal is created to help with the animation mechanism.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const scaleValue = useRef(new Animated.Value(0)).current;
  const [showModal, setShowModal] = useState(visible);

  useEffect(() =&amp;gt; {
    toggleModal();
  }, [visible]);

  const toggleModal = () =&amp;gt; {
    if (visible) {
      setShowModal(true);
      Animated.spring(scaleValue, {
        toValue: 1,
        useNativeDriver: true,
      }).start();
    } else {
      setTimeout(() =&amp;gt; setShowModal(false), 200);
      Animated.timing(scaleValue, {
        toValue: 0,
        duration: 300,
        useNativeDriver: true,
      }).start();
    }
  };

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The full &lt;code&gt;ModalLayout.tsx&lt;/code&gt; will look 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 { View, StyleSheet, Modal, Animated } from "react-native";
import React, { useState, useRef, useEffect } from "react";
import { COLORS } from "../../utils";

type Props = {
  visible: boolean;
  children: ReactNode;
};

const ModalLayout = ({ children, visible }: Props) =&amp;gt; {
  const scaleValue = useRef(new Animated.Value(0)).current;
  const [showModal, setShowModal] = useState(visible);

  useEffect(() =&amp;gt; {
    toggleModal();
  }, [visible]);

  const toggleModal = () =&amp;gt; {
    if (visible) {
      setShowModal(true);
      Animated.spring(scaleValue, {
        toValue: 1,
        useNativeDriver: true,
      }).start();
    } else {
      setTimeout(() =&amp;gt; setShowModal(false), 200);
      Animated.timing(scaleValue, {
        toValue: 0,
        duration: 300,
        useNativeDriver: true,
      }).start();
    }
  };

  return (
    &amp;lt;Modal transparent visible={showModal}&amp;gt;
      &amp;lt;View style={styles.container}&amp;gt;
        &amp;lt;Animated.View
          style={[styles.modalCon, { transform: [{ scale: scaleValue }] }]}
        &amp;gt;
          {children}
        &amp;lt;/Animated.View&amp;gt;
      &amp;lt;/View&amp;gt;
    &amp;lt;/Modal&amp;gt;
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: COLORS.modalbackgroundcolor,
  },

  modalCon: {
    backgroundColor: "white",
    paddingHorizontal: 20,
    paddingVertical: 30,
    borderRadius: 20,
    elevation: 20,
    width: "80%",
  },
});

export default ModalLayout;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Button Component&lt;/p&gt;

&lt;p&gt;&lt;code&gt;create Button.tsx&lt;/code&gt; inside the project directory &lt;code&gt;src/Components/Buttons/Button.tsx&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Text, TouchableOpacity, StyleSheet } from "react-native";
import React from "react";
import { COLORS } from "../../utils";

type ButtonProps = {
  onPress: () =&amp;gt; void;
  title: string;
};

const Button = ({ onPress, title }: ButtonProps) =&amp;gt; {
  return (
    &amp;lt;TouchableOpacity
      onPress={onPress}
      activeOpacity={0.9}
      style={styles.button}
    &amp;gt;
      &amp;lt;Text style={styles.title}&amp;gt;{title}&amp;lt;/Text&amp;gt;
    &amp;lt;/TouchableOpacity&amp;gt;
  );
};

const styles = StyleSheet.create({
  button: {
    height: 55,
    width: "65%",
    backgroundColor: COLORS.red,
    marginVertical: 10,
    justifyContent: "center",
    alignItems: "center",
    borderRadius: 5,
  },
  title: {
    color: COLORS.white,
    fontWeight: "bold",
    fontSize: 18,
  },
});

export default Button;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Modal Screen&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the modal screen import &lt;code&gt;ModalLayout.tsx&lt;/code&gt; and &lt;code&gt;Button.tsx&lt;/code&gt; components inside &lt;code&gt;ModalScreen.tsx&lt;/code&gt; go ahead and import &lt;code&gt;Entypo&lt;/code&gt; from &lt;code&gt;@expo/vector-icons&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";
import { View, StyleSheet, Image, Text, TouchableOpacity } from "react-native";

import { Button } from "../Components/Buttons";
import { Entypo } from "@expo/vector-icons";

import ModalLayout from "../Components/Modals/ModalLayout";

const ModalScreen = () =&amp;gt; {
  const [visible, setVisible] = useState(false);

  return (
    &amp;lt;View style={styles.container}&amp;gt;
      &amp;lt;ModalLayout visible={visible}&amp;gt;
        &amp;lt;View style={{ alignItems: "center" }}&amp;gt;
          &amp;lt;View style={styles.header}&amp;gt;
            &amp;lt;TouchableOpacity onPress={() =&amp;gt; setVisible(false)}&amp;gt;
              &amp;lt;Entypo
                name="cross"
                style={{ height: 30, width: 30 }}
                size={24}
                color="black"
              /&amp;gt;
            &amp;lt;/TouchableOpacity&amp;gt;
          &amp;lt;/View&amp;gt;
        &amp;lt;/View&amp;gt;
        &amp;lt;View style={{ alignItems: "center" }}&amp;gt;
          &amp;lt;Image
            source={require(".././assets/imageIcons/success.png")}
            style={{ height: 150, width: 150, marginVertical: 10 }}
          /&amp;gt;
        &amp;lt;/View&amp;gt;

        &amp;lt;Text style={{ marginVertical: 30, fontSize: 20, textAlign: "center" }}&amp;gt;
          Transaction Successful
        &amp;lt;/Text&amp;gt;
      &amp;lt;/ModalLayout&amp;gt;
      &amp;lt;Button title="Show Modal" onPress={() =&amp;gt; setVisible(true)} /&amp;gt;
    &amp;lt;/View&amp;gt;
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
  },
  header: {
    width: "100%",
    height: 40,
    alignItems: "flex-end",
    justifyContent: "center",
  },
});

export default ModalScreen;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;create &lt;code&gt;src/utils/Colors.tsx&lt;/code&gt; in the project directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const COLORS = {
  modalbackgroundcolor: "rgba(0,0,0,0.5)",
  modalbackgroundContainerColor: "white",
  white: "#FFFFFF",
  red: "#FF0000",
};

export default COLORS;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The custom modal should look like this&lt;/p&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%2Fvwtgz49rpzg9pk86umkq.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%2Fvwtgz49rpzg9pk86umkq.png" width="800" height="1731"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GitHub link to the tutorial: &lt;a href="https://github.com/Victorvikson1996/React-Native-Custom-Modal-" rel="noopener noreferrer"&gt;Github Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The tutorial completes how we can use Animated API and Modal component from react-native to build our custom modal in our mobile projects. kindly share this article and follow on &lt;a href="https://twitter.com/Victorebukaezea" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>How to Setup Environmental Variables in React Native (Expo)</title>
      <dc:creator>Ezea Victor</dc:creator>
      <pubDate>Tue, 07 Dec 2021 21:08:19 +0000</pubDate>
      <link>https://dev.to/victorezea/how-to-setup-environmental-variables-in-react-native-expo-257j</link>
      <guid>https://dev.to/victorezea/how-to-setup-environmental-variables-in-react-native-expo-257j</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For mobile developers using expo or react native (a cross-platform framework for building mobile software applications), you might want to hide your AWS or Stripe API key's before pushing it to GitHub.&lt;/p&gt;

&lt;p&gt;With the following steps, you will learn how to hide your API key&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First Step&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;make sure you have expo installed globally on your machine, you can do so by running one of the following command lines on your terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --global expo-cli
yarn global add expo-cli

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the installation go ahead and create an expo project by running the following command line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;expo init myapi-app

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go ahead and open your project on your IDE project directory terminal and install a package called &lt;a href="https://www.npmjs.com/package/react-native-dotenv" rel="noopener noreferrer"&gt;react-native-dotenv&lt;/a&gt; by running any of the following commands on your project terminal.&lt;br&gt;
&lt;/p&gt;

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

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the installation go ahead to your root project directory and create a file called &lt;strong&gt;.env&lt;/strong&gt; file&lt;/p&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%2Fa4n1hdhxpx0pemd4aq76.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%2Fa4n1hdhxpx0pemd4aq76.png" alt="Screenshot (2).png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open your &lt;strong&gt;.env&lt;/strong&gt; file and set up your API keys (Google, Stripe, MongoDB etc..)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GOOGLE_API_KEY=YOUR_API_KEY

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then go ahead to your **babel.config.js ** file and add the following lines of code to it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = function (api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: [
      [
        "module:react-native-dotenv",
        {
          moduleName: "@env",
          path: ".env",
        }
      ]
    ]
  };
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can now import your API key and use it in your project by calling it, you can do this in your &lt;strong&gt;app.js&lt;/strong&gt; or &lt;strong&gt;index.js&lt;/strong&gt; file in your project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import { View, StyleSheet } from 'react-native';
import { MapView } from 'react-native-maps'
import MapViewDirections from 'react-native-maps-directions';

import { GOOGLE_API_KEY } from '@env'

const App = () =&amp;gt; {
    return (
        &amp;lt;MapView&amp;gt;
            &amp;lt;MapViewDirections
                origin={origin.description}
                destination={destination.description}
                apikey={GOOGLE_API_KEY}
                strokeWidth={3}
                strokeColor="black"

            /&amp;gt;
        &amp;lt;/MapView&amp;gt;
    );
}

const styles = StyleSheet.create({})

export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**Last step **&lt;/p&gt;

&lt;p&gt;Go to your .gitignore file and add &lt;strong&gt;.env&lt;/strong&gt; this will let git ignore your .env file while pushing your code to &lt;strong&gt;GitHub&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node_modules/
.expo/
dist/
npm-debug.*
*.jks
*.p8
*.p12
*.key
*.mobileprovision
*.orig.*
web-build/

# macOS
.DS_Store

.env

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go ahead and run &lt;code&gt;expo start&lt;/code&gt; on your terminal to start your application&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt; You have learnt how to hide your API keys using &lt;strong&gt;.env&lt;/strong&gt; and **react-native-dotenv **package for your expo react-native project&lt;/p&gt;

&lt;p&gt;you can follow me on &lt;a href="https://twitter.com/victorebukaezea" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>deepseek</category>
      <category>python</category>
      <category>ai</category>
      <category>softwaredevelopment</category>
    </item>
  </channel>
</rss>
