<?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>Forem: Babalola Macaulay</title>
    <description>The latest articles on Forem by Babalola Macaulay (@devbabs).</description>
    <link>https://forem.com/devbabs</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%2F765005%2Ff08ab9e9-e68f-4831-844b-0db4599c63c0.jpeg</url>
      <title>Forem: Babalola Macaulay</title>
      <link>https://forem.com/devbabs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/devbabs"/>
    <language>en</language>
    <item>
      <title>Laravel - Advanced Eloquent ORM Techniques</title>
      <dc:creator>Babalola Macaulay</dc:creator>
      <pubDate>Thu, 11 Jul 2024 18:52:46 +0000</pubDate>
      <link>https://forem.com/devbabs/laravel-advanced-eloquent-orm-techniques-129e</link>
      <guid>https://forem.com/devbabs/laravel-advanced-eloquent-orm-techniques-129e</guid>
      <description>&lt;p&gt;Laravel's Eloquent ORM is a very powerful took for interacting with databases, using an expressive fluent syntax. While it's very easy to get quite familiar with the basics of Eloquent and stick to using just the basic techniques, there are advanced techniques that can significantly enhance your application's efficiency and capabilities.&lt;/p&gt;

&lt;p&gt;In this article, I will be talking about these advanced Eloquent ORM techniques, helping you make the most of Laravel's strongly built database interaction layer.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Complex Query Building
&lt;/h3&gt;

&lt;p&gt;Eloquent provides a clean and simple way to build complex SQL queries, beyond just basic CRUD operations. You can use advanced query-building techniques to optimize your database interactions using &lt;strong&gt;Subqueries&lt;/strong&gt; and &lt;strong&gt;Advance Where Clauses&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  SubQueries
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;App\Models\Order&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;App\Models\User&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$latestOrders&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'user_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'created_at'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'created_at'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;selectRaw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'MAX(created_at)'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'orders'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;groupBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'user_id'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nv"&gt;$users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;whereIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$latestOrders&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;pluck&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'user_id'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Advanced Where Clauses
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'status'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'active'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'age'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;orWhere&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'country'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'US'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Relationships and Performance Optimization
&lt;/h3&gt;

&lt;p&gt;Eloquent makes it easy to define relationships between models. However, optimizing these relationships is crucial for performance.&lt;/p&gt;

&lt;h4&gt;
  
  
  Eager Loading
&lt;/h4&gt;

&lt;p&gt;This helps to reduce the number of queries executed by loading related models alongside the main model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'posts'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'comments'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Lazy Eager Loading
&lt;/h4&gt;

&lt;p&gt;Sometimes, you might want to load relationships after the initial query. Lazy eager loading can help in such scenarios.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$someCondition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$users&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'posts'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'comments'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Chunking
&lt;/h4&gt;

&lt;p&gt;For processing large datasets, chunking can help avoid memory exhaustion by breaking the query results into manageable chunks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$users&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$users&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Process user&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Advanced Relationships
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Polymorphic Relationships
&lt;/h4&gt;

&lt;p&gt;Polymorphic relationships allow a model to belong to more than one other model on a single association.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Migration for comments table&lt;/span&gt;
&lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'comments'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Blueprint&lt;/span&gt; &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'body'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'commentable'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;timestamps&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Comment model&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Comment&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;commentable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphTo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Post model&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Comment&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'commentable'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Video model&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Video&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Comment&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'commentable'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Many-to-Many Polymorphic Relationships
&lt;/h4&gt;

&lt;p&gt;Many-to-many polymorphic relationships allow a model to belong to multiple other models and vice versa.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Migration for tags table&lt;/span&gt;
&lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'tags'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Blueprint&lt;/span&gt; &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;timestamps&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Migration for taggables table&lt;/span&gt;
&lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'taggables'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Blueprint&lt;/span&gt; &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;foreignId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'tag_id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;constrained&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'taggable'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;timestamps&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Tag model&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Tag&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphedByMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'taggable'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;videos&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphedByMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Video&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'taggable'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Post model&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphToMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Tag&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'taggable'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Video model&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Video&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;morphToMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Tag&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'taggable'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Mastering advanced Eloquent ORM techniques can significantly enhance your Laravel application's performance and maintainability.&lt;br&gt;
By leveraging complex query building, optimizing relationships, understanding advanced relationships, and a few other Advanced Eloquent ORM Techniques in Laravel, you can build more efficient and scalable applications. &lt;/p&gt;

&lt;p&gt;Cheers 🥂&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>webdev</category>
      <category>backenddevelopment</category>
    </item>
    <item>
      <title>How to create an Animated Toast with React Native Re-Animated</title>
      <dc:creator>Babalola Macaulay</dc:creator>
      <pubDate>Sun, 16 Oct 2022 07:37:52 +0000</pubDate>
      <link>https://forem.com/devbabs/how-to-create-an-animated-toast-with-react-native-re-animated-27o2</link>
      <guid>https://forem.com/devbabs/how-to-create-an-animated-toast-with-react-native-re-animated-27o2</guid>
      <description>&lt;p&gt;In this article, we will be creating our own &lt;strong&gt;Animated Toast&lt;/strong&gt; from scratch in React Native.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpxlybtengs17qunkwu3o.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpxlybtengs17qunkwu3o.jpeg" alt="How to create an Animated Toast with React Native Re-Animated" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;React Native is a popular open-source Javascript framework, created and managed by Meta (They used to be known as Facebook).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;NOTE: This article assumes prior knowledge of basics of react native, and i will not be explaining how to install or setup react native on your computer.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You should know that there's not just one way to achieve animations in React Native. React Native has it's own &lt;code&gt;Animated&lt;/code&gt; library, for really nice fluid animations that are very easy to create.&lt;/p&gt;

&lt;p&gt;However, we will be using &lt;a href="https://docs.swmansion.com/react-native-reanimated/" rel="noopener noreferrer"&gt;React Native Reanimated&lt;/a&gt; created and managed by the awesome team at swmansion.&lt;/p&gt;

&lt;p&gt;Animations done using React Native Reanimated are even more smooth and cooler, as animation and event handling logic off of the JavaScript thread and onto the UI thread.&lt;/p&gt;

&lt;p&gt;Now let's get into real code...&lt;/p&gt;

&lt;p&gt;We'll start by creating our new React Native project by running the code in our terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx react-native init AnimatedToastExample
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Feel free to run your new project on a simulator or a real device using &lt;code&gt;npx react-native run android&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, we install our package for animation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i react-native-reanimated
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To complete our react-native-reanimated setup, go to the root of your project and add this to your &lt;code&gt;babel.config.js&lt;/code&gt; file:&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 = {
    presets: [
      ...
    ],
    plugins: [
      ...
      'react-native-reanimated/plugin',
    ],
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't forget to run &lt;code&gt;npx pod-install&lt;/code&gt; if you are developing for iOS.&lt;/p&gt;

&lt;p&gt;Now we're ready to start animating our React Native Components.&lt;/p&gt;

&lt;h4&gt;
  
  
  Create our button component
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;TouchableOpacity
    style={{
        backgroundColor: 'teal',
        alignSelf: 'center',
        padding: 10,
        marginTop: 40,
        borderRadius: 4
    }}
    onPress={showToast}
&amp;gt;
    &amp;lt;Text style={{ color: 'white' }}&amp;gt;
        Show Toast
    &amp;lt;/Text&amp;gt;
&amp;lt;/TouchableOpacity&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Create our toast component
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;View
    style={[{
        padding: 20,
        position: 'absolute',
        top: 40,
        width: '100%',
        zIndex: 1
    }]}
&amp;gt;
    &amp;lt;View
        style={{
            backgroundColor: 'teal',
            padding: 10,
            borderRadius: 4,
        }}
    &amp;gt;
        &amp;lt;Text style={{ color: 'white' }}&amp;gt;
            This is a toast message
        &amp;lt;/Text&amp;gt;
    &amp;lt;/View&amp;gt;
&amp;lt;/View&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, before we animate our toast, let's import &lt;code&gt;Animated&lt;/code&gt; from our animation library and change our the wrapping &lt;code&gt;View&lt;/code&gt; component to use &lt;code&gt;Animated.View&lt;/code&gt; instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Animated from 'react-native-reanimated';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Change the &lt;code&gt;View&lt;/code&gt; on our component to &lt;code&gt;Animated.View&lt;/code&gt; to 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;&amp;lt;Animated.View
    style={[{
        padding: 20,
        position: 'absolute',
        top: 40,
        width: '100%',
        zIndex: 1
    }]}
&amp;gt;
    &amp;lt;View
        style={{
            backgroundColor: 'teal',
            padding: 10,
            borderRadius: 4,
        }}
    &amp;gt;
        &amp;lt;Text style={{ color: 'white' }}&amp;gt;
            This is a toast message
        &amp;lt;/Text&amp;gt;
    &amp;lt;/View&amp;gt;
&amp;lt;/Animated.View&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, inside the style block of our animated component, we need to add styles that can be animated. What we want to do is change the &lt;code&gt;top&lt;/code&gt; value to a value that makes is completely hidden from view, at the top of our screen.&lt;/p&gt;

&lt;p&gt;Let's import 2 hooks from our animation library: &lt;code&gt;useAnimatedStyle&lt;/code&gt; and &lt;code&gt;useSharedValue&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 { useAnimatedStyle, useSharedValue } from 'react-native-reanimated';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  useSharedValue
&lt;/h5&gt;

&lt;p&gt;This hook allows you to create a value that can be changed at any point in your code, and the change can be listed to by other animation hooks.&lt;br&gt;
It takes the initial value you as the only argument.&lt;/p&gt;
&lt;h4&gt;
  
  
  useAnimatedStyle
&lt;/h4&gt;

&lt;p&gt;This hook takes a callback as it's argument and returns a style object.&lt;br&gt;
This hook will listen for changes in any of the values returned in it's style object, and effect these changes in your view component.&lt;/p&gt;

&lt;p&gt;We will use these 2 hooks to create an initial &lt;code&gt;top&lt;/code&gt; value for our Toast that hides is completely from viwew, and then listen for changes in that value, so that when we click our button and the value is changed, the toast will animate to the new &lt;code&gt;top&lt;/code&gt; value.&lt;/p&gt;
&lt;h4&gt;
  
  
  hide toast container
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const toastTopPosition = useSharedValue(-Dimensions.get('window').height)

const toastAnimatedStyle = useAnimatedStyle(() =&amp;gt; {
    return {
        top: toastTopPosition.value
    }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Add this new style to your &lt;code&gt;Animated.View&lt;/code&gt; component styles.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Animated.View
    style={[
        {
            padding: 20,
            position: 'absolute',
            width: '100%',
            zIndex: 1
        },
        toastAnimatedStyle
    ]}
&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you reload your App, you should see your toast container is now completely hidden.&lt;/p&gt;

&lt;h4&gt;
  
  
  Show toast on button click()
&lt;/h4&gt;

&lt;p&gt;Now we will handle our button click and show our toast immediately. At this point, all we need to complete our animations is to change the value we created with &lt;code&gt;useSharedValue&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;const showToast = () =&amp;gt; {
    toastTopPosition.value = withTiming(40)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will notice that we just used another hook from &lt;code&gt;react-native-reanimated&lt;/code&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  withTiming
&lt;/h5&gt;

&lt;p&gt;This hook helps us to animate a change in our values.&lt;/p&gt;

&lt;h4&gt;
  
  
  Reload your app to see complete changes.
&lt;/h4&gt;

&lt;p&gt;To wrap this up, we'll add a &lt;code&gt;setTimeout&lt;/code&gt; function to our &lt;code&gt;showToast&lt;/code&gt; function to make our toast disappear after a few seconds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const showToast = () =&amp;gt; {
    toastTopPosition.value = withTiming(40)

    setTimeout(() =&amp;gt; {
        toastTopPosition.value = withTiming(-Dimensions.get('window').height)
    }, 2000);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your final code should look something 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 React from 'react';
import {
    SafeAreaView,
    Text,
    TouchableOpacity,
    Dimensions,
    View,
} from 'react-native';
import Animated, { useAnimatedStyle, useSharedValue, withTiming } from 'react-native-reanimated';

import {
    Header,
} from 'react-native/Libraries/NewAppScreen';

const App = () =&amp;gt; {
    const toastTopPosition = useSharedValue(-Dimensions.get('window').height)

    const toastAnimatedStyle = useAnimatedStyle(() =&amp;gt; {
        return {
            top: toastTopPosition.value
        }
    })

    const showToast = () =&amp;gt; {
        toastTopPosition.value = withTiming(40)

        setTimeout(() =&amp;gt; {
            toastTopPosition.value = withTiming(-Dimensions.get('window').height)
        }, 2000);
    }

    return (
        &amp;lt;SafeAreaView style={{ flex: 1 }}&amp;gt;
            &amp;lt;Header /&amp;gt;

            &amp;lt;Animated.View
                style={[
                    {
                        padding: 20,
                        position: 'absolute',
                        width: '100%',
                        zIndex: 1
                    },
                    toastAnimatedStyle
                ]}
            &amp;gt;
                &amp;lt;View
                    style={{
                        backgroundColor: 'teal',
                        padding: 10,
                        borderRadius: 4,
                    }}
                &amp;gt;
                    &amp;lt;Text style={{ color: 'white' }}&amp;gt;
                        This is a toast message
                    &amp;lt;/Text&amp;gt;
                &amp;lt;/View&amp;gt;
            &amp;lt;/Animated.View&amp;gt;

            &amp;lt;View
                style={{
                    flex: 1,
                    backgroundColor: 'white'
                }}
            &amp;gt;
                &amp;lt;TouchableOpacity
                    style={{
                        backgroundColor: 'teal',
                        alignSelf: 'center',
                        padding: 10,
                        marginTop: 40,
                        borderRadius: 4
                    }}
                    onPress={showToast}
                &amp;gt;
                    &amp;lt;Text style={{ color: 'white' }}&amp;gt;
                        Show Toast
                    &amp;lt;/Text&amp;gt;
                &amp;lt;/TouchableOpacity&amp;gt;
            &amp;lt;/View&amp;gt;
        &amp;lt;/SafeAreaView&amp;gt;
    );
};

export default App;

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff6fuu6ljk5ulub5njbv3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff6fuu6ljk5ulub5njbv3.png" alt="React Native ReAnimated Toast" width="800" height="1731"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Feel free to let me know what you think, and if this article has helped you in any way, please don't hesitate to give it a ♡ and share 👍🏽.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to create an Animated Toast with React Native Re-Animated.</title>
      <dc:creator>Babalola Macaulay</dc:creator>
      <pubDate>Sun, 16 Oct 2022 07:32:02 +0000</pubDate>
      <link>https://forem.com/devbabs/how-to-create-an-animated-toast-with-react-native-re-animated-2d8e</link>
      <guid>https://forem.com/devbabs/how-to-create-an-animated-toast-with-react-native-re-animated-2d8e</guid>
      <description>&lt;p&gt;In this article, we will be creating our own &lt;strong&gt;Animated Toast&lt;/strong&gt; from scratch in React Native.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpxlybtengs17qunkwu3o.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpxlybtengs17qunkwu3o.jpeg" alt="How to create an Animated Toast with React Native Re-Animated" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;React Native is a popular open-source Javascript framework, created and managed by Meta (They used to be known as Facebook).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;NOTE: This article assumes prior knowledge of basics of react native, and i will not be explaining how to install or setup react native on your computer.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You should know that there's not just one way to achieve animations in React Native. React Native has it's own &lt;code&gt;Animated&lt;/code&gt; library, for really nice fluid animations that are very easy to create.&lt;/p&gt;

&lt;p&gt;However, we will be using &lt;a href="https://docs.swmansion.com/react-native-reanimated/" rel="noopener noreferrer"&gt;React Native Reanimated&lt;/a&gt; created and managed by the awesome team at swmansion.&lt;/p&gt;

&lt;p&gt;Animations done using React Native Reanimated are even more smooth and cooler, as animation and event handling logic off of the JavaScript thread and onto the UI thread.&lt;/p&gt;

&lt;p&gt;Now let's get into real code...&lt;/p&gt;

&lt;p&gt;We'll start by creating our new React Native project by running the code in our terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx react-native init AnimatedToastExample
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Feel free to run your new project on a simulator or a real device using &lt;code&gt;npx react-native run android&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, we install our package for animation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i react-native-reanimated
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To complete our react-native-reanimated setup, go to the root of your project and add this to your &lt;code&gt;babel.config.js&lt;/code&gt; file:&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 = {
    presets: [
      ...
    ],
    plugins: [
      ...
      'react-native-reanimated/plugin',
    ],
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't forget to run &lt;code&gt;npx pod-install&lt;/code&gt; if you are developing for iOS.&lt;/p&gt;

&lt;p&gt;Now we're ready to start animating our React Native Components.&lt;/p&gt;

&lt;h4&gt;
  
  
  Create our button component
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;TouchableOpacity
    style={{
        backgroundColor: 'teal',
        alignSelf: 'center',
        padding: 10,
        marginTop: 40,
        borderRadius: 4
    }}
    onPress={showToast}
&amp;gt;
    &amp;lt;Text style={{ color: 'white' }}&amp;gt;
        Show Toast
    &amp;lt;/Text&amp;gt;
&amp;lt;/TouchableOpacity&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Create our toast component
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;View
    style={[{
        padding: 20,
        position: 'absolute',
        top: 40,
        width: '100%',
        zIndex: 1
    }]}
&amp;gt;
    &amp;lt;View
        style={{
            backgroundColor: 'teal',
            padding: 10,
            borderRadius: 4,
        }}
    &amp;gt;
        &amp;lt;Text style={{ color: 'white' }}&amp;gt;
            This is a toast message
        &amp;lt;/Text&amp;gt;
    &amp;lt;/View&amp;gt;
&amp;lt;/View&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, before we animate our toast, let's import &lt;code&gt;Animated&lt;/code&gt; from our animation library and change our the wrapping &lt;code&gt;View&lt;/code&gt; component to use &lt;code&gt;Animated.View&lt;/code&gt; instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Animated from 'react-native-reanimated';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Change the &lt;code&gt;View&lt;/code&gt; on our component to &lt;code&gt;Animated.View&lt;/code&gt; to 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;&amp;lt;Animated.View
    style={[{
        padding: 20,
        position: 'absolute',
        top: 40,
        width: '100%',
        zIndex: 1
    }]}
&amp;gt;
    &amp;lt;View
        style={{
            backgroundColor: 'teal',
            padding: 10,
            borderRadius: 4,
        }}
    &amp;gt;
        &amp;lt;Text style={{ color: 'white' }}&amp;gt;
            This is a toast message
        &amp;lt;/Text&amp;gt;
    &amp;lt;/View&amp;gt;
&amp;lt;/Animated.View&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, inside the style block of our animated component, we need to add styles that can be animated. What we want to do is change the &lt;code&gt;top&lt;/code&gt; value to a value that makes is completely hidden from view, at the top of our screen.&lt;/p&gt;

&lt;p&gt;Let's import 2 hooks from our animation library: &lt;code&gt;useAnimatedStyle&lt;/code&gt; and &lt;code&gt;useSharedValue&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 { useAnimatedStyle, useSharedValue } from 'react-native-reanimated';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  useSharedValue
&lt;/h5&gt;

&lt;p&gt;This hook allows you to create a value that can be changed at any point in your code, and the change can be listed to by other animation hooks.&lt;br&gt;
It takes the initial value you as the only argument.&lt;/p&gt;
&lt;h4&gt;
  
  
  useAnimatedStyle
&lt;/h4&gt;

&lt;p&gt;This hook takes a callback as it's argument and returns a style object.&lt;br&gt;
This hook will listen for changes in any of the values returned in it's style object, and effect these changes in your view component.&lt;/p&gt;

&lt;p&gt;We will use these 2 hooks to create an initial &lt;code&gt;top&lt;/code&gt; value for our Toast that hides is completely from viwew, and then listen for changes in that value, so that when we click our button and the value is changed, the toast will animate to the new &lt;code&gt;top&lt;/code&gt; value.&lt;/p&gt;
&lt;h4&gt;
  
  
  hide toast container
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const toastTopPosition = useSharedValue(-Dimensions.get('window').height)

const toastAnimatedStyle = useAnimatedStyle(() =&amp;gt; {
    return {
        top: toastTopPosition.value
    }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Add this new style to your &lt;code&gt;Animated.View&lt;/code&gt; component styles.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Animated.View
    style={[
        {
            padding: 20,
            position: 'absolute',
            width: '100%',
            zIndex: 1
        },
        toastAnimatedStyle
    ]}
&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you reload your App, you should see your toast container is now completely hidden.&lt;/p&gt;

&lt;h4&gt;
  
  
  Show toast on button click()
&lt;/h4&gt;

&lt;p&gt;Now we will handle our button click and show our toast immediately. At this point, all we need to complete our animations is to change the value we created with &lt;code&gt;useSharedValue&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;const showToast = () =&amp;gt; {
    toastTopPosition.value = withTiming(40)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will notice that we just used another hook from &lt;code&gt;react-native-reanimated&lt;/code&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  withTiming
&lt;/h5&gt;

&lt;p&gt;This hook helps us to animate a change in our values.&lt;/p&gt;

&lt;h4&gt;
  
  
  Reload your app to see complete changes.
&lt;/h4&gt;

&lt;p&gt;To wrap this up, we'll add a &lt;code&gt;setTimeout&lt;/code&gt; function to our &lt;code&gt;showToast&lt;/code&gt; function to make our toast disappear after a few seconds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const showToast = () =&amp;gt; {
    toastTopPosition.value = withTiming(40)

    setTimeout(() =&amp;gt; {
        toastTopPosition.value = withTiming(-Dimensions.get('window').height)
    }, 2000);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your final code should look something 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 React from 'react';
import {
    SafeAreaView,
    Text,
    TouchableOpacity,
    Dimensions,
    View,
} from 'react-native';
import Animated, { useAnimatedStyle, useSharedValue, withTiming } from 'react-native-reanimated';

import {
    Header,
} from 'react-native/Libraries/NewAppScreen';

const App = () =&amp;gt; {
    const toastTopPosition = useSharedValue(-Dimensions.get('window').height)

    const toastAnimatedStyle = useAnimatedStyle(() =&amp;gt; {
        return {
            top: toastTopPosition.value
        }
    })

    const showToast = () =&amp;gt; {
        toastTopPosition.value = withTiming(40)

        setTimeout(() =&amp;gt; {
            toastTopPosition.value = withTiming(-Dimensions.get('window').height)
        }, 2000);
    }

    return (
        &amp;lt;SafeAreaView style={{ flex: 1 }}&amp;gt;
            &amp;lt;Header /&amp;gt;

            &amp;lt;Animated.View
                style={[
                    {
                        padding: 20,
                        position: 'absolute',
                        width: '100%',
                        zIndex: 1
                    },
                    toastAnimatedStyle
                ]}
            &amp;gt;
                &amp;lt;View
                    style={{
                        backgroundColor: 'teal',
                        padding: 10,
                        borderRadius: 4,
                    }}
                &amp;gt;
                    &amp;lt;Text style={{ color: 'white' }}&amp;gt;
                        This is a toast message
                    &amp;lt;/Text&amp;gt;
                &amp;lt;/View&amp;gt;
            &amp;lt;/Animated.View&amp;gt;

            &amp;lt;View
                style={{
                    flex: 1,
                    backgroundColor: 'white'
                }}
            &amp;gt;
                &amp;lt;TouchableOpacity
                    style={{
                        backgroundColor: 'teal',
                        alignSelf: 'center',
                        padding: 10,
                        marginTop: 40,
                        borderRadius: 4
                    }}
                    onPress={showToast}
                &amp;gt;
                    &amp;lt;Text style={{ color: 'white' }}&amp;gt;
                        Show Toast
                    &amp;lt;/Text&amp;gt;
                &amp;lt;/TouchableOpacity&amp;gt;
            &amp;lt;/View&amp;gt;
        &amp;lt;/SafeAreaView&amp;gt;
    );
};

export default App;

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff6fuu6ljk5ulub5njbv3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff6fuu6ljk5ulub5njbv3.png" alt="React Native ReAnimated Toast" width="800" height="1731"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Feel free to let me know what you think, and if this article has helped you in any way, please don't hesitate to give it a ♡ and share 👍🏽.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to create an Animated Toast with React Native Re-Animated</title>
      <dc:creator>Babalola Macaulay</dc:creator>
      <pubDate>Sun, 16 Oct 2022 07:09:10 +0000</pubDate>
      <link>https://forem.com/devbabs/how-to-create-an-animated-toast-with-react-native-re-animated-39l3</link>
      <guid>https://forem.com/devbabs/how-to-create-an-animated-toast-with-react-native-re-animated-39l3</guid>
      <description>&lt;p&gt;In this article, we will be creating our own &lt;strong&gt;Animated Toast&lt;/strong&gt; from scratch in React Native.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fpxlybtengs17qunkwu3o.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fpxlybtengs17qunkwu3o.jpeg" alt="How to create an Animated Toast with React Native Re-Animated"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;React Native is a popular open-source Javascript framework, created and managed by Meta (They used to be known as Facebook).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;NOTE: This article assumes prior knowledge of basics of react native, and i will not be explaining how to install or setup react native on your computer.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You should know that there's not just one way to achieve animations in React Native. React Native has it's own &lt;code&gt;Animated&lt;/code&gt; library, for really nice fluid animations that are very easy to create.&lt;/p&gt;

&lt;p&gt;However, we will be using &lt;a href="https://docs.swmansion.com/react-native-reanimated/" rel="noopener noreferrer"&gt;React Native Reanimated&lt;/a&gt; created and managed by the awesome team at swmansion.&lt;/p&gt;

&lt;p&gt;Animations done using React Native Reanimated are even more smooth and cooler, as animation and event handling logic off of the JavaScript thread and onto the UI thread.&lt;/p&gt;

&lt;p&gt;Now let's get into real code...&lt;/p&gt;

&lt;p&gt;We'll start by creating our new React Native project by running the code in our terminal:&lt;/p&gt;

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

npx react-native init AnimatedToastExample


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

&lt;/div&gt;

&lt;p&gt;Feel free to run your new project on a simulator or a real device using &lt;code&gt;npx react-native run android&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, we install our package for animation&lt;/p&gt;

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

npm i react-native-reanimated


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

&lt;/div&gt;

&lt;p&gt;To complete our react-native-reanimated setup, go to the root of your project and add this to your &lt;code&gt;babel.config.js&lt;/code&gt; file:&lt;/p&gt;

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

 module.exports = {
    presets: [
      ...
    ],
    plugins: [
      ...
      'react-native-reanimated/plugin',
    ],
  };


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

&lt;/div&gt;

&lt;p&gt;Don't forget to run &lt;code&gt;npx pod-install&lt;/code&gt; if you are developing for iOS.&lt;/p&gt;

&lt;p&gt;Now we're ready to start animating our React Native Components.&lt;/p&gt;

&lt;h4&gt;
  
  
  Create our button component
&lt;/h4&gt;

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

&amp;lt;TouchableOpacity
    style={{
        backgroundColor: 'teal',
        alignSelf: 'center',
        padding: 10,
        marginTop: 40,
        borderRadius: 4
    }}
    onPress={showToast}
&amp;gt;
    &amp;lt;Text style={{ color: 'white' }}&amp;gt;
        Show Toast
    &amp;lt;/Text&amp;gt;
&amp;lt;/TouchableOpacity&amp;gt;


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

&lt;/div&gt;
&lt;h4&gt;
  
  
  Create our toast component
&lt;/h4&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;View
    style={[{
        padding: 20,
        position: 'absolute',
        top: 40,
        width: '100%',
        zIndex: 1
    }]}
&amp;gt;
    &amp;lt;View
        style={{
            backgroundColor: 'teal',
            padding: 10,
            borderRadius: 4,
        }}
    &amp;gt;
        &amp;lt;Text style={{ color: 'white' }}&amp;gt;
            This is a toast message
        &amp;lt;/Text&amp;gt;
    &amp;lt;/View&amp;gt;
&amp;lt;/View&amp;gt;


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

&lt;/div&gt;

&lt;p&gt;Now, before we animate our toast, let's import &lt;code&gt;Animated&lt;/code&gt; from our animation library and change our the wrapping &lt;code&gt;View&lt;/code&gt; component to use &lt;code&gt;Animated.View&lt;/code&gt; instead.&lt;/p&gt;

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

import Animated from 'react-native-reanimated';


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

&lt;/div&gt;

&lt;p&gt;Change the &lt;code&gt;View&lt;/code&gt; on our component to &lt;code&gt;Animated.View&lt;/code&gt; to look like this:&lt;/p&gt;

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

&amp;lt;Animated.View
    style={[{
        padding: 20,
        position: 'absolute',
        top: 40,
        width: '100%',
        zIndex: 1
    }]}
&amp;gt;
    &amp;lt;View
        style={{
            backgroundColor: 'teal',
            padding: 10,
            borderRadius: 4,
        }}
    &amp;gt;
        &amp;lt;Text style={{ color: 'white' }}&amp;gt;
            This is a toast message
        &amp;lt;/Text&amp;gt;
    &amp;lt;/View&amp;gt;
&amp;lt;/Animated.View&amp;gt;


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

&lt;/div&gt;

&lt;p&gt;Next, inside the style block of our animated component, we need to add styles that can be animated. What we want to do is change the &lt;code&gt;top&lt;/code&gt; value to a value that makes is completely hidden from view, at the top of our screen.&lt;/p&gt;

&lt;p&gt;Let's import 2 hooks from our animation library: &lt;code&gt;useAnimatedStyle&lt;/code&gt; and &lt;code&gt;useSharedValue&lt;/code&gt;:&lt;/p&gt;

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

import { useAnimatedStyle, useSharedValue } from 'react-native-reanimated';


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

&lt;/div&gt;
&lt;h5&gt;
  
  
  useSharedValue
&lt;/h5&gt;

&lt;p&gt;This hook allows you to create a value that can be changed at any point in your code, and the change can be listed to by other animation hooks.&lt;br&gt;
It takes the initial value you as the only argument.&lt;/p&gt;
&lt;h4&gt;
  
  
  useAnimatedStyle
&lt;/h4&gt;

&lt;p&gt;This hook takes a callback as it's argument and returns a style object.&lt;br&gt;
This hook will listen for changes in any of the values returned in it's style object, and effect these changes in your view component.&lt;/p&gt;

&lt;p&gt;We will use these 2 hooks to create an initial &lt;code&gt;top&lt;/code&gt; value for our Toast that hides is completely from viwew, and then listen for changes in that value, so that when we click our button and the value is changed, the toast will animate to the new &lt;code&gt;top&lt;/code&gt; value.&lt;/p&gt;
&lt;h4&gt;
  
  
  hide toast container
&lt;/h4&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const toastTopPosition = useSharedValue(-Dimensions.get('window').height)

const toastAnimatedStyle = useAnimatedStyle(() =&amp;gt; {
    return {
        top: toastTopPosition.value
    }
})


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

&lt;/div&gt;

&lt;p&gt;Add this new style to your &lt;code&gt;Animated.View&lt;/code&gt; component styles.&lt;/p&gt;

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

&amp;lt;Animated.View
    style={[
        {
            padding: 20,
            position: 'absolute',
            width: '100%',
            zIndex: 1
        },
        toastAnimatedStyle
    ]}
&amp;gt;


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

&lt;/div&gt;

&lt;p&gt;Once you reload your App, you should see your toast container is now completely hidden.&lt;/p&gt;

&lt;h4&gt;
  
  
  Show toast on button click()
&lt;/h4&gt;

&lt;p&gt;Now we will handle our button click and show our toast immediately. At this point, all we need to complete our animations is to change the value we created with &lt;code&gt;useSharedValue&lt;/code&gt;&lt;/p&gt;

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

const showToast = () =&amp;gt; {
    toastTopPosition.value = withTiming(40)
}


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

&lt;/div&gt;

&lt;p&gt;You will notice that we just used another hook from &lt;code&gt;react-native-reanimated&lt;/code&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  withTiming
&lt;/h5&gt;

&lt;p&gt;This hook helps us to animate a change in our values.&lt;/p&gt;

&lt;h4&gt;
  
  
  Reload your app to see complete changes.
&lt;/h4&gt;

&lt;p&gt;To wrap this up, we'll add a &lt;code&gt;setTimeout&lt;/code&gt; function to our &lt;code&gt;showToast&lt;/code&gt; function to make our toast disappear after a few seconds.&lt;/p&gt;

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

const showToast = () =&amp;gt; {
    toastTopPosition.value = withTiming(40)

    setTimeout(() =&amp;gt; {
        toastTopPosition.value = withTiming(-Dimensions.get('window').height)
    }, 2000);
}


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

&lt;/div&gt;

&lt;p&gt;Now your final code should look something like this:&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 {
    SafeAreaView,
    Text,
    TouchableOpacity,
    Dimensions,
    View,
} from 'react-native';
import Animated, { useAnimatedStyle, useSharedValue, withTiming } from 'react-native-reanimated';

import {
    Header,
} from 'react-native/Libraries/NewAppScreen';

const App = () =&amp;gt; {
    const toastTopPosition = useSharedValue(-Dimensions.get('window').height)

    const toastAnimatedStyle = useAnimatedStyle(() =&amp;gt; {
        return {
            top: toastTopPosition.value
        }
    })

    const showToast = () =&amp;gt; {
        toastTopPosition.value = withTiming(40)

        setTimeout(() =&amp;gt; {
            toastTopPosition.value = withTiming(-Dimensions.get('window').height)
        }, 2000);
    }

    return (
        &amp;lt;SafeAreaView style={{ flex: 1 }}&amp;gt;
            &amp;lt;Header /&amp;gt;

            &amp;lt;Animated.View
                style={[
                    {
                        padding: 20,
                        position: 'absolute',
                        width: '100%',
                        zIndex: 1
                    },
                    toastAnimatedStyle
                ]}
            &amp;gt;
                &amp;lt;View
                    style={{
                        backgroundColor: 'teal',
                        padding: 10,
                        borderRadius: 4,
                    }}
                &amp;gt;
                    &amp;lt;Text style={{ color: 'white' }}&amp;gt;
                        This is a toast message
                    &amp;lt;/Text&amp;gt;
                &amp;lt;/View&amp;gt;
            &amp;lt;/Animated.View&amp;gt;

            &amp;lt;View
                style={{
                    flex: 1,
                    backgroundColor: 'white'
                }}
            &amp;gt;
                &amp;lt;TouchableOpacity
                    style={{
                        backgroundColor: 'teal',
                        alignSelf: 'center',
                        padding: 10,
                        marginTop: 40,
                        borderRadius: 4
                    }}
                    onPress={showToast}
                &amp;gt;
                    &amp;lt;Text style={{ color: 'white' }}&amp;gt;
                        Show Toast
                    &amp;lt;/Text&amp;gt;
                &amp;lt;/TouchableOpacity&amp;gt;
            &amp;lt;/View&amp;gt;
        &amp;lt;/SafeAreaView&amp;gt;
    );
};

export default App;


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

&lt;/div&gt;




&lt;p&gt;Feel free to let me know what you think, and if this article has helped you in any way, please don't hesitate to give it a ♡ and share 👍🏽.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
