<?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: jakobpotosme</title>
    <description>The latest articles on Forem by jakobpotosme (@jakobpotosme).</description>
    <link>https://forem.com/jakobpotosme</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%2F884724%2F70088938-4bc8-4845-aedf-fc9bfa539518.png</url>
      <title>Forem: jakobpotosme</title>
      <link>https://forem.com/jakobpotosme</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jakobpotosme"/>
    <language>en</language>
    <item>
      <title>What is an NFT?</title>
      <dc:creator>jakobpotosme</dc:creator>
      <pubDate>Sun, 28 Aug 2022 22:11:45 +0000</pubDate>
      <link>https://forem.com/jakobpotosme/what-is-an-nft-5gbk</link>
      <guid>https://forem.com/jakobpotosme/what-is-an-nft-5gbk</guid>
      <description>&lt;p&gt;Upon reading this article you have probably heard of notorious names such as Bored Ape Yacht Club and Crypto Punks where people have had their lives changed by owning a simple JPEG. Leaving you with the question as to what the heck is an NFT? Upon finishing this post, I hope to have answered your question. &lt;/p&gt;

&lt;h3&gt;
  
  
  Non Fungible Token
&lt;/h3&gt;

&lt;p&gt;The word NFT stands for a Non Fungible Token and has revolutionized the way people get paid for their work and show off their creavity. It is a digital token that lives on the blockchain with valuable information attached to it. A fungible item is an an item that can be replaced or is mutually interchangible. For example, 1 dollar can always be exchanged for 1 dollar. However, every non fungible token has a uniqueness that eliminates this interchangibility. To keep it simple it is this uniqueness that creates the NFT. &lt;/p&gt;

&lt;h3&gt;
  
  
  Pokemon are NFT's
&lt;/h3&gt;

&lt;p&gt;This concept may be easier for my fellow gamers to understand. Let us compare this to the world of Pokemon. The world that these pokemon live in is equivalent to the smart contract that lives on the Ethereum network. At the time of writing, there are currently 905 pokemon that exist. Each with their own unique stats, ability, and characteristics. This means that no two pokemon are interchangeble with one another.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JxGJ9r94--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mosqsyh7g97sc3x7ldla.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JxGJ9r94--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mosqsyh7g97sc3x7ldla.png" alt="Comparison" width="880" height="268"&gt;&lt;/a&gt; Each pokemon can be compared to an asset that has been created using the ERC721 Token Standard. This standard has a unique token ID with a corresponding tokenURI. The unique characteristics are defined within the tokenURI which contains the token's information also known as metadata. Here is an example as to how that metadata is constructed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
"name":"Pikachu",
"description":"Gen 1 Pokemon",
"image": "Pikachu_Image_URI",
"attributes": [
  "type":"electric"
]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why care about NFT's?
&lt;/h3&gt;

&lt;p&gt;A valid question I had to ask myself numerous times. The current state of NFT's is flawed. They are driven off hype and feel like a game of hot potato with whoever is willing to pay more. However, if we take a step back and look at the core technology involved there is much more to it. Everything in life is based on agreements and that is the beauty of smart contracts. Smart Contracts are unbreakable agreements that live on a decentralized network where anyone can see the terms of agreement. I believe NFT's have untapped potential that will release in due time. (Stay tuned to future posts for my opinions on this matter)&lt;/p&gt;

&lt;h3&gt;
  
  
  Mint your own NFT
&lt;/h3&gt;

&lt;p&gt;Now after reading this basic overview about NFT's, it is time to mint your own! You will be interacting with a simple Dapp I created that will give you a color corresponding to the current month &lt;a href="https://dynamic-month-nft-front-end.vercel.app/"&gt;here&lt;/a&gt;. If you are completely new to the Web3 ecosystem read this article first to set up your wallet &lt;a href="https://medium.com/@decryptmedia/metamask-the-beginners-guide-6111143f2581"&gt;Metamask Guide&lt;/a&gt;. After setting up your wallet you will need to switch to Rinkeby Network in order to interact with the Dapp. In order to mint, you will need some test Eth to pay for your NFT which you can get &lt;a href="https://faucets.chain.link/rinkeby"&gt;here&lt;/a&gt;. Once your wallet is funded, simply click mint and follow the prompt. After about 20 seconds, you should see a notification popup stating that the mint was successful!&lt;br&gt;
You can then view your newly minted NFT &lt;a href="https://testnets.opensea.io/account"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;I hope you leave this post with new gained knowledge along with an open mind to the concept of NFT's. I believe they are just getting started and will have a huge impact in our future. &lt;/p&gt;

&lt;h4&gt;
  
  
  Further Reading
&lt;/h4&gt;

&lt;p&gt;You may have noticed that I solely focused on the Ethereum network. The smart contract is not tied to just the Ethereum network, there are several others such as Solana and Cardano with smart contract capabilites. If you are curious I encourage you to read up on pros / cons of each.&lt;/p&gt;

&lt;h5&gt;
  
  
  References
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=gyMwXuJrbJQ&amp;amp;t=40387s"&gt;Patrick Collins&lt;/a&gt;&lt;/p&gt;

</description>
      <category>web3</category>
      <category>blockchain</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Supabase &amp; SWR</title>
      <dc:creator>jakobpotosme</dc:creator>
      <pubDate>Fri, 01 Jul 2022 20:55:34 +0000</pubDate>
      <link>https://forem.com/jakobpotosme/supabase-swr-3icp</link>
      <guid>https://forem.com/jakobpotosme/supabase-swr-3icp</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hello everyone&lt;/strong&gt;! &lt;br&gt;
Today we will incorporate the SWR library to fetch data updates in realtime using React Hooks. While working on a project, I realized that changes made within my database were not reflected on my site unless I preformed a refresh. This bothered me as user's are expecting for changes to be made in real time. &lt;/p&gt;

&lt;p&gt;After some researching, I stumbled upon SWR which is a react hook used for data fetching. A short TLDR is that &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;SWR is a strategy to first return the data from cache , then send a fetch request to revalidate the data, and finally come up with the most recent data&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A more comprehensive overview can be found &lt;a href="https://swr.vercel.app/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;SWR seemed to have exactly what I needed so I began implementation.&lt;/p&gt;

&lt;p&gt;I will be recreating my particular situation here using dummy data but with the same technologies. (Next Js, Supabase, SWR)&lt;/p&gt;

&lt;p&gt;We first need to set up our NextJS application.&lt;/p&gt;

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

npx create-next-app name-of-project


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

&lt;/div&gt;

&lt;p&gt;Follow the prompts and once installed simply cd over to the newly created project &lt;/p&gt;

&lt;h2&gt;
  
  
  Installing Tailwind CSS
&lt;/h2&gt;

&lt;p&gt;We will be adding tailwind css to the application as well in order to aid us with our styling. &lt;/p&gt;

&lt;p&gt;Run the following two commands&lt;/p&gt;

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

npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p


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

&lt;/div&gt;

&lt;p&gt;The second command will create the 'tailwind.config.js' file for you, it is a config JS file for Tailwind CSS.&lt;/p&gt;

&lt;p&gt;Copy and past the content below which will tell Tailwind CSS which files to watch out for.&lt;/p&gt;

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

module.exports = {
  content: [
    "./pages/**/*.{js,ts,jsx,tsx}",
    "./components/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}


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

&lt;/div&gt;

&lt;p&gt;We then will have to add this to our globals.css file.&lt;/p&gt;

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

@tailwind base;
@tailwind components;
@tailwind utilities;


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

&lt;/div&gt;

&lt;p&gt;BOOM! Our Nextjs application is now configured to use TailwindCSS!&lt;/p&gt;

&lt;p&gt;Finally, run the following command &lt;/p&gt;

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

npm run dev


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

&lt;/div&gt;

&lt;p&gt;this will spin up the Nextjs application on your &lt;a href="https://localhost:3000" rel="noopener noreferrer"&gt;https://localhost:3000&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our local environment is now ready to go! Time to set up Supabase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up Supabase
&lt;/h2&gt;

&lt;p&gt;Go to the official Supabase &lt;a href="https://supabase.com" rel="noopener noreferrer"&gt;website&lt;/a&gt; and sign in. &lt;/p&gt;

&lt;p&gt;Click &lt;strong&gt;+ New Project&lt;/strong&gt; and then click &lt;strong&gt;+ New Organization&lt;/strong&gt; to create your new project team. Then enter a name to create your organization. &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%2Fs9pjx0s95ryx0i5qjk5m.png" 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%2Fs9pjx0s95ryx0i5qjk5m.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once your organization is created, we can proceed to create the project.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Name: Pick whichever you'd like&lt;/li&gt;
&lt;li&gt;Database Password: Enter a strong password but save it as you may need it in the future!&lt;/li&gt;
&lt;li&gt;Region: Choose region closest to you&lt;/li&gt;
&lt;li&gt;Pricing Plan: Free will suffice, you can always upgrade later.&lt;/li&gt;
&lt;/ul&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%2Fsp87d32dgv8pvbw0s99m.png" 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%2Fsp87d32dgv8pvbw0s99m.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally hit Create new Project, and there we have our new Supabase project! Lets now create our tables.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Tables
&lt;/h2&gt;

&lt;p&gt;Click the Table Editior from the left panel of the dashboard. Then click Create a new table in the center of the screen.&lt;/p&gt;

&lt;p&gt;A side panel will appear and we will fill it out as follows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Name: Pick whichever you'd like&lt;/li&gt;
&lt;li&gt;Description: Optional&lt;/li&gt;
&lt;li&gt;Enable Row Level Security (RLS) :  We will skip at this time, useful for more complex applications but beyond the scope of this post&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enable Realtime: We will skip at this time, this may replace SWR but at this time I have not messed with it enough. (A new blog will come soon once I have a better understanding )&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;id: Unique identifier with a type uuid with the default value of uuid_generate_v4()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;title: The title of post&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;content: The content of post&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Connecting to Supabase
&lt;/h2&gt;

&lt;p&gt;Next step is to connect our application to the supabase client with the use of the supabase-js dependency.&lt;/p&gt;

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

npm install @supabase/supabase-js


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

&lt;/div&gt;

&lt;p&gt;We must create a .env file in the root of our project and add the following lines to it.&lt;/p&gt;

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

NEXT_PUBLIC_SUPABASE_URL=YOUR_SUPABASE_URL
NEXT_PUBLIC_SUPABASE_ANON_KEY=YOUR_SUPABASE_ANON_KEY


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

&lt;/div&gt;

&lt;p&gt;The SUPABASE_URL and SUPABASE_ANON_KEY can be found by following these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to the "Settings" section of your project.&lt;/li&gt;
&lt;li&gt;Click "API" in the sidebar.&lt;/li&gt;
&lt;li&gt;Find your API URL in this page.&lt;/li&gt;
&lt;li&gt;Find your "anon" and "service_role" keys on this page.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now restart the server in order for the changes made in the &lt;strong&gt;.env&lt;/strong&gt; file to reflect properly.&lt;/p&gt;

&lt;p&gt;Now that the API credentials are in place, lets create a helper file to initialize the Supabase client. Create a utils folder in the root of your project and within that folder, create a supabaseClient.js file. Paste the following inside.&lt;/p&gt;

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

import { createClient } from '@supabase/supabase-js'

const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY

export const supabase = createClient(supabaseUrl, supabaseAnonKey)


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

&lt;/div&gt;

&lt;p&gt;Now whenever we need to interact with Supabase, we can simply call upon by importing &lt;strong&gt;utils/supabase.js&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating first post
&lt;/h2&gt;

&lt;p&gt;Our index page is currently just the default so lets change it up. Copy and paste the code below to have&lt;/p&gt;

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

&amp;lt;div className={styles.container}&amp;gt;
      &amp;lt;Head&amp;gt;
        &amp;lt;title&amp;gt;Create Next App&amp;lt;/title&amp;gt;
        &amp;lt;meta name="description" content="Generated by create next app" /&amp;gt;
        &amp;lt;link rel="icon" href="/favicon.ico" /&amp;gt;
      &amp;lt;/Head&amp;gt;

      &amp;lt;main className='flex items-center justify-center h-screen flex-col'&amp;gt;
        &amp;lt;h1 className="text-3xl font-bold "&amp;gt;
          Create a post!
        &amp;lt;/h1&amp;gt;

        &amp;lt;form className=' p-20 flex gap-8'&amp;gt;

          &amp;lt;input type='text' className='shadow p-2 border-b w-full outline-none' placeholder='Type here...'&amp;gt;&amp;lt;/input&amp;gt;
          &amp;lt;button type='submit' className='bg-blue-300 p-2 rounded' &amp;gt;Submit&amp;lt;/button&amp;gt;
        &amp;lt;/form&amp;gt;
      &amp;lt;/main&amp;gt;
    &amp;lt;/div&amp;gt;


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Inserting post into Supabase
&lt;/h2&gt;

&lt;p&gt;In order for our post to reach Supabase, there are a few things we need.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Import the supabase client &lt;/li&gt;
&lt;li&gt;Adding a title,content state to hold our values.&lt;/li&gt;
&lt;li&gt;Adding an onSubmit function to handle that data.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Add the following to your code.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import { supabase } from '../utils/supabaseClient'

...

const [title, setTitle] = useState('')
  const [content,setContent] = useState('')

  const onSubmit = async (e) =&amp;gt; {
    e.preventDefault()

    try {
      const {data, error} = await supabase
          .from('Posts')
          .insert([
            {title: title, content: content}
          ])

          if(data){
            console.log('success')
          }

    } catch (error) {
      console.log(error)
    }


    setTitle('')
    setContent('')
  }

...

&amp;lt;form onSubmit={onSubmit} className=' p-20 flex flex-col gap-8'&amp;gt;


          &amp;lt;input type='text' className='shadow p-2 border-b w-full outline-none' 
          placeholder='Title...' onChange={e =&amp;gt; setTitle(e.target.value)} value={title} &amp;gt;&amp;lt;/input&amp;gt;
          &amp;lt;input type='text' className='shadow p-2 border-b w-full outline-none' 
          placeholder='Content...' onChange={e =&amp;gt; setContent(e.target.value)} value={content} &amp;gt;&amp;lt;/input&amp;gt;
          &amp;lt;button type='submit' className='bg-blue-300 p-2 rounded' &amp;gt;Submit&amp;lt;/button&amp;gt;
        &amp;lt;/form&amp;gt;



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

&lt;/div&gt;

&lt;p&gt;Fill out and submit the form. Now if everything is working correctly you should see a 'success' message in your console and the data on your Supabase table!&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%2Fplsi949etubn3ckk6du7.png" 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%2Fplsi949etubn3ckk6du7.png" alt="Image description"&gt;&lt;/a&gt;&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%2Fjcitceys1y5wsk2bnkwd.png" 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%2Fjcitceys1y5wsk2bnkwd.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Great! Now lets retrieve and display the data within our application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Retrieving Data from Supabase
&lt;/h2&gt;

&lt;p&gt;Next steps will be creating a components folder at the root of your project. Within that folder, create a Posts.js file. &lt;/p&gt;

&lt;p&gt;Copy and paste the code below &lt;/p&gt;

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

import React from 'react'

const Posts = ({title,content}) =&amp;gt; {
  return (
    &amp;lt;div className='p-10 bg-zinc-200 rounded w-1/3 mb-10 '&amp;gt;
      &amp;lt;div className='flex flex-col gap-y-5'&amp;gt;
      &amp;lt;h1 className='text-xl font-bold'&amp;gt;{title}&amp;lt;/h1&amp;gt;
      &amp;lt;p className='font-semibold'&amp;gt;{content}&amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;


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

export default Posts


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

&lt;/div&gt;

&lt;p&gt;This will be our Posts component and we are passing it the &lt;strong&gt;title ** and **content&lt;/strong&gt; props that we will see shortly.&lt;/p&gt;

&lt;p&gt;Back in our index.js file, we will be adding a getTableData function that will retrieve all the posts from supabase and storing it within our posts state.&lt;/p&gt;

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

  const [posts, setPosts] = useState() 

  ...

  const getTableData = async () =&amp;gt;{

    try {
      const {data, error} = await supabase
          .from('Posts')
          .select('*')

          if(data){

            setPosts(data)
          }

    } catch (error) {
      console.log(error)
    }
  }

 useEffect(()=&amp;gt;{

    getTableData()
  }, [])



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

&lt;/div&gt;

&lt;p&gt;We must invoke this function by calling it within a useEffect hook. This will call the function only once, when the page is first rendered. &lt;/p&gt;

&lt;p&gt;After importing the Posts component at the top of the application, add the following code below to your index.js file.&lt;/p&gt;

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

import Posts from '../components/Posts'

...

&amp;lt;div className='pb-20 flex flex-col items-center justify-center '&amp;gt;
        {posts &amp;amp;&amp;amp; posts.map(elem =&amp;gt;{
          return(&amp;lt;Posts key={elem.id} title={elem.title} content={elem.content} /&amp;gt;)
        })}
 &amp;lt;/div&amp;gt;



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

&lt;/div&gt;

&lt;p&gt;This will display the data only when the posts state is &lt;strong&gt;not&lt;/strong&gt; null. We are then mapping through the data and passing these props:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;key : &lt;a href="https://reactjs.org/docs/lists-and-keys.html#keys" rel="noopener noreferrer"&gt;Elements identity&lt;/a&gt; or (unique identifier)&lt;/li&gt;
&lt;li&gt;title : title of post&lt;/li&gt;
&lt;li&gt;content : content of post&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can now successfully view our data but only if we manually refresh. This is where SWR steps in. &lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing SWR
&lt;/h2&gt;

&lt;p&gt;Lets begin by installing SWR and refactoring our code a bit.&lt;/p&gt;

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

npm install swr


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

&lt;/div&gt;

&lt;p&gt;Now we could leave our code as is and leave everything on client side, however lets take advantage of what makes Nextjs so powerful.&lt;br&gt;
We will move all our api related tasks into the api directory.&lt;/p&gt;

&lt;p&gt;Create a new file &lt;strong&gt;posts.js&lt;/strong&gt; and paste the code found below within.&lt;/p&gt;

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

import { supabase } from "../../utils/supabaseClient";

const PostsApi = async (req,res) =&amp;gt;{

    if(req.method === 'GET'){

        const {data,error} = await supabase.from('Posts')
                                    .select('*')

        if(error){
            return res.status(500).json({message: error})
        }

        return res.status(200).json({data})
    }

}
export default PostsApi



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

&lt;/div&gt;

&lt;p&gt;Lets go over what is going on so far.&lt;/p&gt;

&lt;p&gt;We have imported our supabase client in order to retrieve neccessary data. There is a PostsApi asychronous function taking in a &lt;strong&gt;req&lt;/strong&gt; which is a request being made to the API and &lt;strong&gt;res&lt;/strong&gt; which is a response based off the request made. If a 'GET' request has been made, we will then retrieve the data from Supabase and return it. &lt;/p&gt;

&lt;p&gt;Now we can interact with our API by fetching 'api/posts'!&lt;/p&gt;

&lt;h2&gt;
  
  
  Refactoring index.js
&lt;/h2&gt;

&lt;p&gt;Now that our API is complete, we can now remove our getTableData and useEffect to make use of SWR.&lt;br&gt;
&lt;strong&gt;Remove the following code&lt;/strong&gt;&lt;/p&gt;

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

const [posts, setPosts] = useState()

...

const getTableData = async () =&amp;gt;{

    try {
      const {data, error} = await supabase
          .from('Posts')
          .select('*')

          if(data){

            setPosts(data)
          }

    } catch (error) {
      console.log(error)
    }
  }

  useEffect(()=&amp;gt;{

    getTableData()
  }, [])


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Replace it with&lt;/strong&gt;&lt;/p&gt;

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

import useSWR from 'swr'

const fetcher = (url) =&amp;gt; fetch(url, {method: 'GET'}).then(res =&amp;gt; res.json());

...

const {data: posts ,error} = useSWR('api/posts', fetcher)


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

&lt;/div&gt;

&lt;p&gt;Finally we just need to wrap our application in the context SWRConfig providing our global configuration options. Head over to your _app.js file and add the following &lt;/p&gt;

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

import '../styles/globals.css'
import { SWRConfig } from 'swr'

function MyApp({ Component, pageProps }) {
  return (&amp;lt;SWRConfig
        value={{ 
            refreshInterval: 10000,
          }}
        &amp;gt;
    &amp;lt;Component {...pageProps} /&amp;gt;
  &amp;lt;/SWRConfig&amp;gt;)
}

export default MyApp


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

&lt;/div&gt;

&lt;p&gt;In our configuration, we are simply telling SWR to refresh our data every 10 seconds. There are several other options that are available to use and they can be found &lt;a href="https://swr.vercel.app/docs/options" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  All done!
&lt;/h2&gt;

&lt;p&gt;Woohoo! Our application now can successfully insert data and see it in real time how exciting! In summary, we have created a Nextjs / Tailwind CSS / Supabase application accompanied with the SWR library to help with our data fetching. A gif is provided below to see it in action.&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%2F4j627nipcjpp0vdivp4c.gif" 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%2F4j627nipcjpp0vdivp4c.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have only scratched the surface of a full on CRUD application and there is certainly room for improvements but I will leave that up to you to experiment and learn on your own.&lt;/p&gt;

&lt;p&gt;I hope you have found this helpful and I hope to see you on my next post. Take care!&lt;/p&gt;

</description>
      <category>swr</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>nextjs</category>
    </item>
  </channel>
</rss>
