<?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: CiaraMaria</title>
    <description>The latest articles on DEV Community by CiaraMaria (@mariaverse).</description>
    <link>https://dev.to/mariaverse</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%2F395350%2F4df1cc20-0dbc-43df-9452-01c9fa6b3c2a.png</url>
      <title>DEV Community: CiaraMaria</title>
      <link>https://dev.to/mariaverse</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mariaverse"/>
    <language>en</language>
    <item>
      <title>Developing a Full-Stack Project on Stacks with Clarity Smart Contracts and Stacks.js Part III: Frontend</title>
      <dc:creator>CiaraMaria</dc:creator>
      <pubDate>Thu, 06 Oct 2022 16:22:53 +0000</pubDate>
      <link>https://dev.to/mariaverse/developing-a-full-stack-project-on-stacks-with-clarity-smart-contracts-and-stacksjs-part-iii-frontend-heo</link>
      <guid>https://dev.to/mariaverse/developing-a-full-stack-project-on-stacks-with-clarity-smart-contracts-and-stacksjs-part-iii-frontend-heo</guid>
      <description>&lt;h2&gt;
  
  
  Frontend
&lt;/h2&gt;

&lt;p&gt;In the terminal from the root &lt;code&gt;gm&lt;/code&gt; directory run:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;





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

&lt;/div&gt;



&lt;p&gt;Before we start writing code, let's take a quick look at the features we'll be using from Stacks.js.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.hiro.so/stacks-js" rel="noopener noreferrer"&gt;Stacks.js&lt;/a&gt; is a full featured JavaScript library for dApps on Stacks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;@stacks/connect&lt;/strong&gt; allows devs to connect web applications to Stacks wallet browser extensions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;@stacks/transactions&lt;/strong&gt; allows for interactions with the smart contract functions and post conditions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;@stacks/network&lt;/strong&gt; is a network and API library for working with Stacks blockchain nodes.&lt;/p&gt;

&lt;p&gt;In your &lt;code&gt;index.js&lt;/code&gt; file, replace the boilerplate code with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Head from "next/head";
import ConnectWallet from "../components/ConnectWallet";
import styles from "../styles/Home.module.css";

export default function Home() {
  return (
    &amp;lt;div className={styles.container}&amp;gt;
      &amp;lt;Head&amp;gt;
        &amp;lt;title&amp;gt;gm&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={styles.main}&amp;gt;
        &amp;lt;h1 className={styles.title}&amp;gt;gm&amp;lt;/h1&amp;gt;

        &amp;lt;div className={styles.components}&amp;gt;
          {/* ConnectWallet file: `../components/ConnectWallet.js` */}
          &amp;lt;ConnectWallet /&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/main&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now run &lt;code&gt;npm start&lt;/code&gt; and navigate to &lt;code&gt;localhost:3000&lt;/code&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%2Fu9re486an6fr89lfjq9g.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%2Fu9re486an6fr89lfjq9g.png" alt="Image description" width="800" height="658"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have a super simple landing page with &lt;a href="https://wallet.hiro.so/" rel="noopener noreferrer"&gt;Hiro wallet&lt;/a&gt; login. We won't focus on styling for this example.&lt;/p&gt;

&lt;p&gt;Looking at the code you'll see the &lt;code&gt;ConnectWallet&lt;/code&gt; component has been created already. This is included as part of the Stacks.js Starters. &lt;/p&gt;

&lt;p&gt;Let's go into the &lt;code&gt;components&lt;/code&gt; directory. Here we see two files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;ConnectWallet.js&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ContractCallVote.js&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're curious about &lt;code&gt;ContractCallVote&lt;/code&gt; you can add the component to &lt;code&gt;index.js&lt;/code&gt; and try it out. For this example we won't be using it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ConnectWallet.js&lt;/code&gt; contains an &lt;code&gt;authenticate&lt;/code&gt; function that creates a &lt;code&gt;userSession&lt;/code&gt;. This will give us the information we need to send to our contract after a user signs in.&lt;/p&gt;

&lt;p&gt;Inside this file comment out these lines:&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;p&amp;gt;mainnet: {userSession.loadUserData().profile.stxAddress.mainnet}&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;testnet: {userSession.loadUserData().profile.stxAddress.testnet}&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This doesn't affect functionality, we just don't want the addresses on the page.&lt;/p&gt;

&lt;p&gt;Now, we are going to create a new component.&lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;component&lt;/code&gt; directory create a file called &lt;code&gt;ContractCallGm.js&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 { useCallback, useEffect, useState } from "react";
import { useConnect } from "@stacks/connect-react";
import { StacksMocknet } from "@stacks/network";
import styles from "../styles/Home.module.css";

import {
  AnchorMode,
  standardPrincipalCV,
  callReadOnlyFunction,
  makeStandardSTXPostCondition,
  FungibleConditionCode
} from "@stacks/transactions";
import { userSession } from "./ConnectWallet";
import useInterval from "@use-it/interval";

const ContractCallGm = () =&amp;gt; {
  const { doContractCall } = useConnect();
  const [ post, setPost ] = useState("");
  const [ hasPosted, setHasPosted ] = useState(false);

  function handleGm() {
    const postConditionAddress = userSession.loadUserData().profile.stxAddress.testnet;
    const postConditionCode = FungibleConditionCode.LessEqual;
    const postConditionAmount = 1 * 1000000;
    doContractCall({
      network: new StacksMocknet(),
      anchorMode: AnchorMode.Any,
      contractAddress: "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM",
      contractName: "gm",
      functionName: "say-gm",
      functionArgs: [],
      postConditions: [
        makeStandardSTXPostCondition(
          postConditionAddress,
          postConditionCode,
          postConditionAmount
        )
      ],
      onFinish: (data) =&amp;gt; {
        console.log("onFinish:", data);
        console.log("Explorer:", `localhost:8000/txid/${data.txId}?chain=testnet`)
      },
      onCancel: () =&amp;gt; {
        console.log("onCancel:", "Transaction was canceled");
      },
    });
  }

  const getGm = useCallback(async () =&amp;gt; {

    if (userSession.isUserSignedIn()) {
      const userAddress = userSession.loadUserData().profile.stxAddress.testnet
      const options = {
          contractAddress: "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM",
          contractName: "gm",
          functionName: "get-gm",
          network: new StacksMocknet(),
          functionArgs: [standardPrincipalCV(userAddress)],
          senderAddress: userAddress
      };

      const result = await callReadOnlyFunction(options);
      console.log(result);
      if (result.value) {
        setHasPosted(true)
        setPost(result.value.data)
      }
    }
  });

  useEffect(() =&amp;gt; {
    getGm();
  }, [userSession.isUserSignedIn()])

  useInterval(getGm, 10000);

  if (!userSession.isUserSignedIn()) {
    return null;
  }

  return (
    &amp;lt;div&amp;gt;
      {!hasPosted &amp;amp;&amp;amp;
        &amp;lt;div&amp;gt;
          &amp;lt;h1 className={styles.title}&amp;gt;Say gm to everyone on Stacks! 👋&amp;lt;/h1&amp;gt;
          &amp;lt;button className="Vote" onClick={() =&amp;gt; handleGm()}&amp;gt;
            gm
          &amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;  
      }
      {hasPosted &amp;amp;&amp;amp; 
        &amp;lt;div&amp;gt;
          &amp;lt;h1&amp;gt;{userSession.loadUserData().profile.stxAddress.testnet} says {post}!&amp;lt;/h1&amp;gt;
        &amp;lt;/div&amp;gt;
      }
    &amp;lt;/div&amp;gt;
  ); 
};

export default ContractCallGm;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are a few things going on here so let's break it down.&lt;/p&gt;

&lt;p&gt;The first function &lt;code&gt;handleGm&lt;/code&gt; is where the bulk of the work is being done.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  function handleGm() {
    const postConditionAddress = userSession.loadUserData().profile.stxAddress.testnet;
    const postConditionCode = FungibleConditionCode.LessEqual;
    const postConditionAmount = 1 * 1000000;
    doContractCall({
      network: new StacksMocknet(),
      anchorMode: AnchorMode.Any,
      contractAddress: "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM",
      contractName: "gm",
      functionName: "say-gm",
      functionArgs: [],
      postConditions: [
        makeStandardSTXPostCondition(
          postConditionAddress,
          postConditionCode,
          postConditionAmount
        )
      ],
      onFinish: (data) =&amp;gt; {
        console.log("onFinish:", data);
        console.log("Explorer:", `localhost:8000/txid/${data.txId}?chain=testnet`)
      },
      onCancel: () =&amp;gt; {
        console.log("onCancel:", "Transaction was canceled");
      },
    });
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function will execute on click of a button. &lt;/p&gt;

&lt;p&gt;The first portion of this function is making the actual contract call to our &lt;code&gt;say-gm&lt;/code&gt; function via &lt;code&gt;doContractCall&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We pass to it the required options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;network&lt;/code&gt;: this is telling &lt;code&gt;doContractCall&lt;/code&gt; what network to use to broadcast the function. There is mainnet, testnest, and devnet. We will be working with devnet and the network config for that is &lt;code&gt;new StacksMocknet()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;anchorMode&lt;/code&gt;: this specifies whether the tx should be included in an anchor block or a microblock. In our case, it doesn't matter which.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;contractAddress&lt;/code&gt;: this is the standard principal that deploys the contract (notice it is the same as the one provided in Devnet.toml).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;functionName&lt;/code&gt;: this is the function you want to call&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;functionArgs&lt;/code&gt;: any parameters required for the function being called.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;postConditions&lt;/code&gt;: Post conditions are a feature unique to Clarity which allow a developer to set conditions which must be met for a transaction to complete execution and materialize to the chain.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you want to learn more about Post Conditions, check out &lt;a href="https://dev.to/krgrs/understanding-stacks-post-conditions-e65"&gt;this&lt;/a&gt; great post.&lt;/p&gt;

&lt;p&gt;We are using a standard STX post condition which is saying that the user will transfer less than or equal to 1 STX or the transaction will abort.&lt;/p&gt;

&lt;p&gt;Upon successful broadcast, &lt;code&gt;onFinish&lt;/code&gt; gets executed. We are simply logging some data.&lt;/p&gt;

&lt;p&gt;Now let's take a look at the next function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const getGm = useCallback(async () =&amp;gt; {

    if (userSession.isUserSignedIn()) {
      const userAddress = userSession.loadUserData().profile.stxAddress.testnet
      const options = {
          contractAddress: "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM",
          contractName: "gm",
          functionName: "get-gm",
          network: new StacksMocknet(),
          functionArgs: [standardPrincipalCV(userAddress)],
          senderAddress: userAddress
      };

      const result = await callReadOnlyFunction(options);
      console.log(result);
      if (result.value) {
        setHasPosted(true)
        setPost(result.value.data)
      }
    }
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is making a call to our &lt;code&gt;read-only&lt;/code&gt; function &lt;code&gt;get-gm&lt;/code&gt;. You'll notice the formatting is slightly different, but the required options are the same ones we just discussed.&lt;/p&gt;

&lt;p&gt;The main difference is that this is a callback function which we will call on an interval to check whether the user has called &lt;code&gt;say-gm&lt;/code&gt;. Remember, &lt;code&gt;get-gm&lt;/code&gt; will return &lt;code&gt;none&lt;/code&gt; unless &lt;code&gt;say-gm&lt;/code&gt; has successfully executed.&lt;/p&gt;

&lt;p&gt;That's really all there is to it! The rest of the code is just React + JSX. Make sure you include this component in &lt;code&gt;index.js&lt;/code&gt; and you're ready to move on to full interaction! &lt;/p&gt;

&lt;p&gt;Boot up DevNet and once it's running, you can start your frontend app with &lt;code&gt;npm start&lt;/code&gt; and navigate to &lt;code&gt;localhost:3000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Click Connect Wallet to login.&lt;/p&gt;

&lt;p&gt;This will open a pop-up with your web wallet accounts.&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%2Fj44xcgx9vxoi2ewbgqfq.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%2Fj44xcgx9vxoi2ewbgqfq.png" alt="Image description" width="800" height="1166"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Notice that it shows Devnet in the top right corner and the account that you configured to DevNet earlier now has a balance of 100M STX. This is your test faucet from the &lt;code&gt;Devnet.toml&lt;/code&gt; file.  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After logging in, your page should update and look like this:&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%2F7m7xm16u84ly37wyuufi.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%2F7m7xm16u84ly37wyuufi.png" alt="Image description" width="800" height="521"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's make a call to our &lt;code&gt;say-gm&lt;/code&gt; function by clicking the purple button! Open your console in browser so you can see the logs.&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%2Ffuqnhwzgw8rssyb9bx9a.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%2Ffuqnhwzgw8rssyb9bx9a.png" alt="Image description" width="800" height="1590"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You'll get the above tx request. It shows the &lt;code&gt;postCondition&lt;/code&gt; we defined, the function being called, and fees.&lt;/p&gt;

&lt;p&gt;Upon confirm, if everything went well you should see something like this in the browser console:&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%2Fwdo2hum4h8jmb3v61961.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%2Fwdo2hum4h8jmb3v61961.png" alt="Image description" width="783" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;onFinish&lt;/code&gt; and &lt;code&gt;Explorer&lt;/code&gt; logs are coming from the &lt;code&gt;handleGm&lt;/code&gt; function. The &lt;code&gt;{type:}&lt;/code&gt; is coming from the &lt;code&gt;getGm&lt;/code&gt; callback. As you can see, it pinged every 10 seconds and it took just less than a minute for our tx call to broadcast and reflect.&lt;/p&gt;

&lt;p&gt;Once that result comes in, the page should update to reflect that &lt;code&gt;UserPost&lt;/code&gt; has updated on chain to map your address with the string "gm".&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%2Ftuj93vxv00dnokenn86o.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%2Ftuj93vxv00dnokenn86o.png" alt="Image description" width="800" height="526"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There it is! A front to back Stacks application from scratch.&lt;/p&gt;

&lt;p&gt;One last fun feature to explore: copy the URL from the &lt;code&gt;Explorer&lt;/code&gt; log and paste it in your browser. This will give you a visual of what the transaction call would look like on the &lt;a href="https://explorer.stacks.co/?chain=mainnet" rel="noopener noreferrer"&gt;Stacks Explorer&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I do hope this has been helpful in understanding the fundamentals of creating a full-stack project on Stacks from developing and testing the smart contracts to setting up a site that users can use to make on-chain calls and simulating what that might look like. Let me know what you think! 🚀&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Shameless plug: find me on Twitter &lt;a href="https://twitter.com/themariaverse" rel="noopener noreferrer"&gt;@themariaverse&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>clarity</category>
      <category>stacks</category>
      <category>web3</category>
      <category>react</category>
    </item>
    <item>
      <title>Developing a Full-Stack Project on Stacks with Clarity Smart Contracts and Stacks.js Part II: Backend</title>
      <dc:creator>CiaraMaria</dc:creator>
      <pubDate>Thu, 06 Oct 2022 16:22:16 +0000</pubDate>
      <link>https://dev.to/mariaverse/developing-a-full-stack-project-on-stacks-with-clarity-smart-contracts-and-stacksjs-part-ii-backend-1pnp</link>
      <guid>https://dev.to/mariaverse/developing-a-full-stack-project-on-stacks-with-clarity-smart-contracts-and-stacksjs-part-ii-backend-1pnp</guid>
      <description>&lt;h2&gt;
  
  
  Backend
&lt;/h2&gt;

&lt;p&gt;Our contract will simply post "gm" from a user to the chain for a small fee. We will do this by mapping the string "gm" to the users unique STX address. The functionality will be basic but enough to demonstrate concepts, testing, and frontend interaction.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;gm.clar&lt;/code&gt; goes the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; gm
;; smart contract that posts a GM to the chain for 1 STX

;; constants
(define-constant CONTRACT_OWNER tx-sender)
(define-constant PRICE u1000000)
(define-constant ERR_STX_TRANSFER (err u100))

;; data maps and vars
(define-data-var total-gms uint u0)
(define-map UserPost principal (string-ascii 2))

;; public functions
(define-read-only (get-total-gms)
  (var-get total-gms)
)

(define-read-only (get-gm (user principal))
  (map-get? UserPost user)
)

(define-public (say-gm)
  (begin
    (unwrap! (stx-transfer? PRICE tx-sender CONTRACT_OWNER) ERR_STX_TRANSFER)
    (map-set UserPost tx-sender "gm")
    (var-set total-gms (+ (var-get total-gms) u1))
    (ok "Success")
  )
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! The entire contract and all the functionality needed is in the above code.&lt;/p&gt;

&lt;p&gt;We have data space at the top of the file and all of the functions listed after.&lt;/p&gt;

&lt;h3&gt;
  
  
  Data:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;define-constant&lt;/code&gt;: We have 3 constants defined. A constant is simply an immutable piece of data, meaning it cannot be changed once defined. In our case, we are using constants to define the contract deployer (I will go into this concept more in a bit), the price of writing the message denoted in microstacks (1,000,000 microstacks is equal to 1 STX), and an error response.
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;define-data-var&lt;/code&gt;: A variable is a piece of data that can be changed by means of future calls. They are, however, only modifiable by the contract in which they are defined. We are defining a variable to track the total number of writes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;define-map&lt;/code&gt;: A map is a data structure that allows you to map keys to values. We will be mapping a principal (Stacks wallet address) to the "gm" string. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Functions:
&lt;/h3&gt;

&lt;p&gt;We have 3 functions, 2 read-only and 1 public.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;read-only&lt;/code&gt; function can only perform read operations. It cannot write or make changes to the chain. As you can see, our &lt;code&gt;read-only&lt;/code&gt; functions are simply grabbing the value of our &lt;code&gt;variable&lt;/code&gt; and &lt;code&gt;map&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, we'll take a line-by-line look at the &lt;code&gt;say-gm&lt;/code&gt; public function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(define-public (say-gm)
  (begin
    (unwrap! (stx-transfer? PRICE tx-sender CONTRACT_OWNER) ERR_STX_TRANSFER)
    (map-set UserPost tx-sender "gm")
    (var-set total-gms (+ (var-get total-gms) u1))
    (ok "Success")
  )
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A Clarity custom function takes the following form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(define-public function-signature function-body)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function definition can be any of the three Clarity function types: &lt;code&gt;public&lt;/code&gt;, &lt;code&gt;private&lt;/code&gt;, or &lt;code&gt;read-only&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The function signature is made up of the function name and any input parameters taken by the function.&lt;/p&gt;

&lt;p&gt;The function body contains the function logic, is limited to one expression, and in the case of a &lt;code&gt;public-function&lt;/code&gt; &lt;strong&gt;must&lt;/strong&gt; return a response type of &lt;code&gt;ok&lt;/code&gt; or &lt;code&gt;err&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Our function signature is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(define-public (say-gm))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our function body is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   (begin
    (unwrap! (stx-transfer? PRICE tx-sender CONTRACT_OWNER) ERR_STX_TRANSFER)
    (map-set UserPost tx-sender "gm")
    (var-set total-gms (+ (var-get total-gms) u1))
    (ok "Success")
  )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What exactly is happening here?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;begin&lt;/code&gt; is one of the Clarity &lt;a href="https://docs.stacks.co/docs/write-smart-contracts/clarity-language/language-functions"&gt;built-in functions&lt;/a&gt;. Recall that I mentioned the function body is limited to one expression, we use &lt;code&gt;begin&lt;/code&gt; to evaluate multiple expressions in a single function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(unwrap! (stx-transfer? PRICE tx-sender CONTRACT_OWNER) ERR_STX_TRANSFER)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are a couple of things happening in this line so let's break it into two:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(stx-transfer? PRICE tx-sender CONTRACT_OWNER)&lt;/code&gt; is using another built-in function &lt;code&gt;stx-transfer?&lt;/code&gt; which increases the STX balance of the recipient by debiting the sender.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;PRICE&lt;/code&gt; is a constant that was defined at the top the file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;tx-sender&lt;/code&gt; is a Clarity keyword representing the address that called the function.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;CONTRACT_OWNER&lt;/code&gt; is also a constant that was defined at the top of the file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;... But wait a minute!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(define-constant CONTRACT_OWNER tx-sender)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;CONTRACT_OWNER&lt;/code&gt; &lt;strong&gt;is&lt;/strong&gt; &lt;code&gt;tx-sender&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/SVgKToBLI6S6DUye1Y/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/SVgKToBLI6S6DUye1Y/giphy.gif" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tx-sender&lt;/code&gt; returns the address of the transaction sender, but it's context changes based on where and how it is used.&lt;/p&gt;

&lt;p&gt;In the case of &lt;code&gt;CONTRACT_OWNER&lt;/code&gt;, &lt;code&gt;tx-sender&lt;/code&gt; will take the context of the standard principal that deployed the contract a.k.a the contract deployer.&lt;/p&gt;

&lt;p&gt;Whereas within the &lt;code&gt;say-gm&lt;/code&gt; function, &lt;code&gt;tx-sender&lt;/code&gt; has the context of the standard principal that is calling into the function.&lt;/p&gt;

&lt;p&gt;You can also manually change the context by using the &lt;code&gt;as-contract&lt;/code&gt; built-in function to set tx-sender to the contract principal.&lt;/p&gt;

&lt;p&gt;I will demonstrate this when we do our manual testing to give you a visual. &lt;/p&gt;

&lt;p&gt;For now let's get back to the first expression.&lt;/p&gt;

&lt;p&gt;The expression will return &lt;code&gt;(ok true)&lt;/code&gt; if the transfer is successful, else it will return an &lt;code&gt;(err)&lt;/code&gt; response. This is where &lt;code&gt;unwrap!&lt;/code&gt; comes in. As the name suggests, it attempts to unwrap the result of the argument passed to it. Unwrapping is extracting the inner value and returning it. If the response is &lt;code&gt;(ok ...)&lt;/code&gt; it will return the inner value otherwise it returns a throw value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(unwrap! (stx-transfer? PRICE tx-sender CONTRACT_OWNER) ERR_STX_TRANSFER)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So there we are unwrapping the result of &lt;code&gt;stx-transfer?&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(map-set UserPost tx-sender "gm")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;map-set&lt;/code&gt; sets the value of the input key to the input value. As part of this function call, we are setting the corresponding value of "gm" to the key of the standard principal calling the function.&lt;/p&gt;

&lt;p&gt;Then:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(var-set total-gms (+ (var-get total-gms) u1))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;var-set&lt;/code&gt; sets the value of the input variable to the expression passed as the second parameter. Here we are performing an incremental count to increase &lt;code&gt;total-gms&lt;/code&gt; by one each time this function executes successfully.&lt;/p&gt;

&lt;p&gt;Finally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(ok "Success")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;because we must return a response type at the end of a function and I just want a success message on completion.&lt;/p&gt;

&lt;h2&gt;
  
  
  Manual Contract Calls
&lt;/h2&gt;

&lt;p&gt;To make sure our contract is working we'll do a few things.&lt;/p&gt;

&lt;p&gt;Run:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;This checks your contract's syntax for errors. If all is well, you should return this message:&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%2F6c1idecqkyb9ecb4pt1y.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%2F6c1idecqkyb9ecb4pt1y.png" alt="Image description" width="800" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now run:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;You should see the following tables:&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%2Fomda37yzon8yfeksjasu.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%2Fomda37yzon8yfeksjasu.png" alt="Image description" width="800" height="679"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first table contains the contract identifier (which is also the contract principal) and the available functions.&lt;/p&gt;

&lt;p&gt;The second table contains 10 sets of dummy principals and balances. This data is pulled directly from the &lt;code&gt;Devnet.toml&lt;/code&gt; file. If you open that file and compare the standard principals, you will notice they are the same.&lt;/p&gt;

&lt;p&gt;From here we can make some contract calls to ensure the desired functionality of each function is there.&lt;/p&gt;

&lt;p&gt;To make a contract call, we follow the format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(contract-call? .contract-id function-name function-params)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make the following call to our &lt;code&gt;say-gm&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(contract-call? .gm say-gm)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Did you get an &lt;code&gt;err u100&lt;/code&gt;? We set our constant &lt;code&gt;ERR_STX_TRANSFER&lt;/code&gt; to &lt;code&gt;err u100&lt;/code&gt;. So why did we get this?&lt;/p&gt;

&lt;p&gt;It is because we just attempted to transfer STX between the same addresses.&lt;/p&gt;

&lt;p&gt;When we run &lt;code&gt;clarinet console&lt;/code&gt;, &lt;code&gt;tx-sender&lt;/code&gt; is automatically set to the contract deployer. You can verify this by running &lt;code&gt;tx-sender&lt;/code&gt; from within the console. If you compare this to the data inside of &lt;code&gt;Devnet.toml&lt;/code&gt; you'll see that the address is the same as the one listed under &lt;code&gt;[accounts.deployer]&lt;/code&gt;. It is also the first address in the table that loads when opening console.&lt;/p&gt;

&lt;p&gt;This comes back to the "context" I mentioned above. You can think of that first address as the deployer and all subsequent addresses as "users" that can call into your function.&lt;/p&gt;

&lt;p&gt;We're going to want to change the &lt;code&gt;tx-sender&lt;/code&gt; within console. You can do this by running&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;::set_tx_sender address
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can copy/paste any address from the assets table or &lt;code&gt;Devnet.toml&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's call &lt;code&gt;get-total-gms&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;(contract-call? .gm get-total-gms)
&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%2Fef8cehqndytbl5017knj.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%2Fef8cehqndytbl5017knj.png" alt="Image description" width="800" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another error? &lt;code&gt;use of unresolved contract&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is happening because we changed our &lt;code&gt;tx-sender&lt;/code&gt; we now have to explicitly state the contract-id as part of the contract call. You can grab this from the assets table. If you cleared the console and need to bring it up again run:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Now try the following call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.gm get-total-gms)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should return &lt;code&gt;u0&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;(contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.gm get-gm tx-sender)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should return &lt;code&gt;none&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Good this is expected because we have not yet called any write functions. &lt;/p&gt;

&lt;p&gt;Alright, let's call &lt;code&gt;say-gm&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;(contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.gm say-gm)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We should see &lt;code&gt;(ok "Success")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now if we make calls again to &lt;code&gt;get-total-gms&lt;/code&gt; and &lt;code&gt;get-gm&lt;/code&gt; we should get &lt;code&gt;u1&lt;/code&gt; and &lt;code&gt;(some "gm")&lt;/code&gt; respectively which means the functionality works!&lt;/p&gt;

&lt;p&gt;You can also run:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;This is will bring up the assets table and you will be able to see the transfer of STX between addresses reflected in the balances.&lt;/p&gt;

&lt;p&gt;Great! We've now manually tested our functions. Before moving on, we're going to do one more thing to demonstrate the context for &lt;code&gt;tx-sender&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I mentioned the &lt;code&gt;as-contract&lt;/code&gt; built-in function and how it changes the context of &lt;code&gt;tx-sender&lt;/code&gt; from the standard principal to the contract principal.&lt;/p&gt;

&lt;p&gt;Exit the console and replace your &lt;code&gt;CONTRACT_OWNER&lt;/code&gt; definition with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(define-constant CONTRACT_OWNER (as-contract tx-sender))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now run &lt;code&gt;clarinet console&lt;/code&gt; again and make the following contract-call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(contract-call? .gm say-gm)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We didn't get an error this time and didn't have to change the &lt;code&gt;tx-sender&lt;/code&gt;. Why?&lt;/p&gt;

&lt;p&gt;Running&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;will provide an answer.&lt;/p&gt;

&lt;p&gt;Notice a &lt;strong&gt;new&lt;/strong&gt; address has been added to the table. A contract principal. So even though we made the call from the deployer, we were able to transfer STX because &lt;code&gt;CONTRACT_OWNER&lt;/code&gt; was initialized as the contract and so we aren't transferring STX between the same address, we are transferring from the deployer's standard principal to the contract principal which is capable of holding tokens as well.&lt;/p&gt;

&lt;p&gt;This demonstrates what the &lt;code&gt;as-contract&lt;/code&gt; function does.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unit Tests
&lt;/h2&gt;

&lt;p&gt;Unit testing is a critical part of smart contract development. Due to the nature of smart contract and the potential use cases, as smart contract developers we want to ensure that we account for as many scenarios as possible. &lt;/p&gt;

&lt;p&gt;Tests are written in typescript and a template is auto generated for you.&lt;/p&gt;

&lt;p&gt;Let's take a look at &lt;code&gt;gm_test.ts&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 { Clarinet, Tx, Chain, Account, types } from 'https://deno.land/x/clarinet@v0.31.0/index.ts';
import { assertEquals } from 'https://deno.land/std@0.90.0/testing/asserts.ts';

Clarinet.test({
    name: "Ensure that &amp;lt;...&amp;gt;",
    async fn(chain: Chain, accounts: Map&amp;lt;string, Account&amp;gt;) {
        let block = chain.mineBlock([
            /* 
             * Add transactions with: 
             * Tx.contractCall(...)
            */
        ]);
        assertEquals(block.receipts.length, 0);
        assertEquals(block.height, 2);

        block = chain.mineBlock([
            /* 
             * Add transactions with: 
             * Tx.contractCall(...)
            */
        ]);
        assertEquals(block.receipts.length, 0);
        assertEquals(block.height, 3);
    },
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is your boilerplate test code. I recommend browsing the Deno documentation provided at the top the file.&lt;/p&gt;

&lt;p&gt;We are just going to write a single basic test together as part of this example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Clarinet.test({
    name: "A user can say gm",
    async fn(chain: Chain, accounts: Map&amp;lt;string, Account&amp;gt;) {
        const deployer = accounts.get('deployer')!.address;
        const user = accounts.get('wallet_1')!.address

        let block = chain.mineBlock([
            Tx.contractCall(
                "gm",
                "say-gm",
                [],
                user
            )
        ]);
        assertEquals(block.receipts.length, 1);
        assertEquals(block.height, 2);

        const messageMapped = chain.callReadOnlyFn(
            "gm",
            "get-gm",
            [types.principal(user)],
            user
        )
        assertEquals(messageMapped.result, types.some(types.ascii("gm")));

        const totalCountIncreased = chain.callReadOnlyFn(
            "gm",
            "get-total-gms",
            [],
            user
        )
        assertEquals(totalCountIncreased.result, types.uint(1));
    }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we're adding a few things to the boilerplate code provided.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const deployer = accounts.get('deployer')!.address;
const user = accounts.get('wallet_1')!.address
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is grabbing the standard principals from &lt;code&gt;Devnet.toml&lt;/code&gt; that correspond to the string we pass to &lt;code&gt;accounts.get()&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;Tx.contractCall(
 "gm",
 "say-gm",
 [],
 user
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Tx.contractCall&lt;/code&gt; is how we can simulate a contract call from our test. It takes 4 parameters:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The contract name&lt;/li&gt;
&lt;li&gt;The function name&lt;/li&gt;
&lt;li&gt;Any params accepted by the function as an array&lt;/li&gt;
&lt;li&gt;The address calling the function
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;assertEquals(block.receipts.length, 1);
assertEquals(block.height, 2);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to also simulate a block being mined. Our test assumes a start at genesis block 1.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;block.receipts.length&lt;/code&gt; accounts for the number of transactions in that block.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;block.height&lt;/code&gt; accounts for the block height at mining. &lt;/p&gt;

&lt;p&gt;In this case we are calling one tx and mining one block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const messageMapped = chain.callReadOnlyFn(
 "gm",
 "get-gm",
 [types.principal(user)],
 user
)
assertEquals(messageMapped.result, types.some(types.ascii("gm")));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are calling a &lt;code&gt;read-only&lt;/code&gt; function with &lt;code&gt;chain.callReadOnlyFn()&lt;/code&gt; which takes the same parameters as &lt;code&gt;Tx.contractCall()&lt;/code&gt; and we are asserting that the result is an ascii string "gm". Why? If the &lt;code&gt;say-gm&lt;/code&gt; call is successful we can assume that the result of this &lt;code&gt;get-gm&lt;/code&gt; will be that string mapped to the user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const totalCountIncreased = chain.callReadOnlyFn(
  "gm",
  "get-total-gms",
  ],
  user
)
assertEquals(totalCountIncreased.result, types.uint(1));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we make a call to &lt;code&gt;get-total-gms&lt;/code&gt; and assert that the total has been incremented to 1. &lt;/p&gt;

&lt;p&gt;Now in the terminal you can run&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;You should see a successful pass:&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%2F2jvxefy2vc21rpsd4m67.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%2F2jvxefy2vc21rpsd4m67.png" alt="Image description" width="800" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Clarinet offers an extensive &lt;a href="https://github.com/hirosystems/clarinet#execute-a-test-suite"&gt;testing suite&lt;/a&gt; that includes &lt;code&gt;lcov&lt;/code&gt; code coverage reports.&lt;/p&gt;

&lt;p&gt;Curious about TDD for contracts? Check out this &lt;a href="https://dev.to/stacks/test-driven-stacks-development-with-clarinet-2e4i"&gt;blog&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Spinning up DevNet
&lt;/h2&gt;

&lt;p&gt;Our backend is complete! Let's get DevNet running and hook it up to our web wallet so that it's ready to go after we build the frontend. &lt;/p&gt;

&lt;p&gt;Make sure Docker is running.&lt;/p&gt;

&lt;p&gt;In your terminal run:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;With this command, Clarinet fetches the appropriate Docker images for the Bitcoin node, Stacks node, Stacks API node, Hyperchain node, and the Bitcoin and Stacks Explorers.&lt;/p&gt;

&lt;p&gt;Boot up can take several minutes the first time you launch.&lt;/p&gt;

&lt;p&gt;When complete, you should see something like this:&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%2Fz5qpo11db35fkh7n1f0g.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%2Fz5qpo11db35fkh7n1f0g.png" alt="Image description" width="800" height="293"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Great! You've got DevNet running. You can read more about the interface in the &lt;a href="https://docs.hiro.so/smart-contracts/devnet#devnet-interface"&gt;docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There is some configuration left to do before you can interact with your frontend app.&lt;/p&gt;

&lt;p&gt;You need to import information from your Hiro &lt;a href="https://wallet.hiro.so/"&gt;web wallet&lt;/a&gt; into your &lt;code&gt;Devnet.toml&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;code&gt;Devnet.toml&lt;/code&gt; is not listed in &lt;code&gt;.gitignore&lt;/code&gt; meaning the information you add to the configuration may be  visible, so you want to either add the file to &lt;code&gt;.gitignore&lt;/code&gt;, or create a separate wallet for testing if you plan to push your code to GitHub.&lt;/p&gt;

&lt;p&gt;From your browser open up your web wallet and change your network to Devnet. This setting can be found by clicking the three dots on the top right corner and selecting Change Network.&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%2Fubmrtuy1b8k19nfzvuj5.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%2Fubmrtuy1b8k19nfzvuj5.png" alt="Image description" width="788" height="740"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Devnet will only be available for selection while you have your local DevNet running.&lt;/p&gt;

&lt;p&gt;Once you change networks, open up the menu again and click View Secret Key. You need to copy this and paste it in &lt;code&gt;Devnet.toml&lt;/code&gt; under &lt;code&gt;[accounts.wallet_1]&lt;/code&gt; where it says &lt;code&gt;mnemonic=&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You will be replacing the generated keys with the ones copied from your web wallet.&lt;/p&gt;

&lt;p&gt;That's it! Configuration is complete and you're ready to start building the frontend app.&lt;/p&gt;

</description>
      <category>clarity</category>
      <category>stacks</category>
      <category>web3</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Developing a Full-Stack Project on Stacks with Clarity Smart Contracts and Stacks.js Part I: Intro and Project Setup</title>
      <dc:creator>CiaraMaria</dc:creator>
      <pubDate>Thu, 06 Oct 2022 16:21:39 +0000</pubDate>
      <link>https://dev.to/mariaverse/developing-a-full-stack-project-on-stacks-with-clarity-smart-contracts-and-stacksjs-part-i-intro-and-project-setup-23bg</link>
      <guid>https://dev.to/mariaverse/developing-a-full-stack-project-on-stacks-with-clarity-smart-contracts-and-stacksjs-part-i-intro-and-project-setup-23bg</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;I am a software developer at Whitelabel Co and a current Resident with the Stacks Foundation to deliver and scale developer education around the Clarity smart contract language and building on Stacks. I became involved in the Stacks community late Spring of 2021 under a pseudonym and have since been involved with multiple projects throughout the ecosystem as a web developer and advisor. Since starting the residency, one of the most common pieces of feedback I've received from new developers to Stacks is a desire for more comprehensive walkthroughs of the full-stack development process.&lt;/p&gt;

&lt;p&gt;I won't get into the Stacks blockchain itself as part of this series, but if you are not familiar with the network check out this &lt;a href="https://www.stacks.co/learn/introduction"&gt;intro&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We'll walk through the fundamental steps of creating a front to back application on Stacks. I will demonstrate a simple smart contract in Clarity, the language used to write smart contracts on Stacks, go over unit testing, practice manual contract calls in clarinet console using &lt;a href="https://www.hiro.so/clarinet"&gt;Clarinet&lt;/a&gt;, a CLI package and developer environment for creating smart contracts, build a frontend using &lt;a href="https://docs.hiro.so/stacksjs-starters"&gt;Stacks.js Starters&lt;/a&gt; to quickly bootstrap a frontend app, hook up the contract to the client, and run a local DevNet instance to demonstrate the application in action as if it was on-chain.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mainnet vs Testnet vs DevNet
&lt;/h2&gt;

&lt;p&gt;It's important to understand the difference between the various "nets" and when you would interact with each one. For Stacks there are three "nets", they are all separate chains and each hold their own data.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Mainnet&lt;/code&gt; is the main Stacks blockchain network. This is fully public and where users interact with deployed smart contracts. In web2 development lingo, you can think of an app on mainnet as in production, prior to which it will be in staging on testnet. The &lt;a href="https://explorer.stacks.co/"&gt;Stacks explorer&lt;/a&gt; is a browser based tool that allows users to access information on Stacks mainnet. Here you can see things like recent blocks, pending and confirmed transactions, as well as the deployed smart contracts themselves.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Testnet&lt;/code&gt; is a separate blockchain from the Stacks mainnet that can be thought of as a sort of staging environment. It is used by developers to test their apps and smart contracts and allows users to interact with the application using test STX tokens rather than their actual tokens. The &lt;a href="https://explorer.stacks.co/"&gt;Stacks explorer&lt;/a&gt; allows users to switch to testnet view by clicking &lt;code&gt;Network&lt;/code&gt; on the top right of the page and selecting &lt;code&gt;stacks.co testnet&lt;/code&gt;. There is also a testnet &lt;a href="https://explorer.stacks.co/sandbox/deploy?chain=testnet"&gt;Sandbox&lt;/a&gt; available for writing and deploying, making contract calls, and receiving test STX via the STX faucet.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;DevNet&lt;/code&gt; is an integration feature provided by our friends at &lt;a href="https://www.hiro.so/"&gt;Hiro&lt;/a&gt; that allows us to spin up a simulated blockchain locally. This allows developers to perform frontend integration and testing without having to deploy to a public tesnet. DevNet is useful when you are in the early stages of development or buidling incognito.&lt;/p&gt;

&lt;p&gt;In this tutorial we will be using &lt;code&gt;DevNet&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;In order to run DevNet there are a few things you'll need to have installed. 👇&lt;/p&gt;

&lt;h3&gt;
  
  
  Clarinet
&lt;/h3&gt;

&lt;p&gt;You'll need to install Clarinet. The detailed instructions can be found in the &lt;a href="https://docs.hiro.so/smart-contracts/clarinet"&gt;Hiro docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Quickstart:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;macOS
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew install clarinet
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;windows
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;winget install clarinet
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can verify whether it has successfully installed by running the following command:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Docker
&lt;/h3&gt;

&lt;p&gt;You'll also need Docker on your machine for running DevNet. Instructions can be found in the &lt;a href="https://docs.docker.com/get-docker/"&gt;Docker docs&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project structure
&lt;/h2&gt;

&lt;p&gt;From the terminal, make sure you are in your desired starting directory and run:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;This will create a main directory for our project which will hold our frontend and backend folders. Move into this directory with:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Initialize the frontend directory with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm create stacks --template frontend&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will spin up a Next app called "frontend" with Stacks.js scaffolding which makes getting started a breeze. Writing the frontend code will be the final step in this example, so we will come back to that directory later.&lt;/p&gt;

&lt;p&gt;Now to initialize the backend directory; for readability in separating the contracts from the client, I'll call this clarinet project "backend":&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;clarinet new backend
&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%2F6mxfyn0vsa8faqguchj5.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%2F6mxfyn0vsa8faqguchj5.png" alt="Image description" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, running the &lt;code&gt;clarinet new&lt;/code&gt; command will generate a number of files and directories. Let's briefly look at the main ones.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Clarinet.toml&lt;/code&gt;: This is your configuration file. Within this file you'll notice &lt;code&gt;[contracts.gm] path = "contracts/gm.clar"&lt;/code&gt;, any contract dependencies whether from contracts you create or import from those deployed on mainnet will show up here.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;/settings/Devnet.toml&lt;/code&gt;: This file contains dummy data which you can use for testing your contract. It provides you with a deployer and a number of wallets. We will be interacting closely with this file throughout this example.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;/contracts&lt;/code&gt; and &lt;code&gt;/tests&lt;/code&gt;: These directories are initialized but remain empty until you create your first contract. We'll come back to these after the next step.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;p&gt;and create your contract with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;clarinet contract new gm
&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%2F2s3zivux60de8y4otqf0.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%2F2s3zivux60de8y4otqf0.png" alt="Image description" width="800" height="191"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, the &lt;code&gt;/contracts&lt;/code&gt; and &lt;code&gt;/tests&lt;/code&gt; directories have files.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;contracts/gm.clar&lt;/code&gt;: This is where the Clarity code goes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;tests/gm_test.ts&lt;/code&gt;: This is where the Typescript unit tests that correspond to each contract go.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Great! Your Clarity smart contract project is set up.&lt;br&gt;
From here you can write your code inside of &lt;code&gt;gm.clar&lt;/code&gt; and write unit tests inside of &lt;code&gt;gm_test.ts&lt;/code&gt;&lt;/p&gt;

</description>
      <category>clarity</category>
      <category>stacks</category>
      <category>web3</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>My Introduction to CSS Animations: Using Inkscape to Create Text Animation</title>
      <dc:creator>CiaraMaria</dc:creator>
      <pubDate>Fri, 01 Jan 2021 21:05:46 +0000</pubDate>
      <link>https://dev.to/mariaverse/my-introduction-to-css-animations-using-inkscape-to-create-text-animation-kp9</link>
      <guid>https://dev.to/mariaverse/my-introduction-to-css-animations-using-inkscape-to-create-text-animation-kp9</guid>
      <description>&lt;p&gt;I awoke one December morning with a single thought in my head: "I want to create a handwriting animation." It was a consuming idea and I quickly got out of bed, made some coffee --a mandatory obligation as a self-proclaimed coffee enthusiast (if that's even a thing?)-- and settled in front of my laptop with the same thought in my head: "I want to create a handwriting animation."&lt;/p&gt;

&lt;p&gt;I had generated my ideal early morning working environment and as I put my hands to the keyboard it hit me; &lt;em&gt;I had no idea what I was doing.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  From Dense Fog to Slightly Less Dense Fog
&lt;/h3&gt;

&lt;p&gt;If there is one thing I've had to radically accept as a developer, it's the feeling of not knowing how to do something. Luckily, we live in a time where nearly all of the world's information is available via teh interwebz. &lt;/p&gt;

&lt;p&gt;The first few hours of that morning were spent searching terms like: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to make a handwriting animation CSS&lt;/li&gt;
&lt;li&gt;CSS animation&lt;/li&gt;
&lt;li&gt;SVG&lt;/li&gt;
&lt;li&gt;How to create SVG&lt;/li&gt;
&lt;li&gt;Online SVG maker&lt;/li&gt;
&lt;li&gt;Adobe Illustrator&lt;/li&gt;
&lt;li&gt;Adobe Illustrator free&lt;/li&gt;
&lt;li&gt;Illustrator alternatives&lt;/li&gt;
&lt;li&gt;How to use Inkscape&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's a little snippet into my thought process and path toward learning that I would first need to draw up my own "handwritten" text so that I can then bring it to life with CSS. &lt;/p&gt;

&lt;h3&gt;
  
  
  Inkscape
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://inkscape.org/" rel="noopener noreferrer"&gt;Inkscape&lt;/a&gt; is a free and open-source vector graphics editor used to create vector images. Upon researching a bit more about Inkscape and how it stands up to Illustrator, it appears to be well-liked among designers and a legitimate alternative for someone who is not interested in paying for proprietary software at the moment (e.g., yours truly). As someone who has not previously worked with Illustrator and fairly new to Inkscape, I can't speak on the pros and cons of each, but I can say that I was able to successfully use Inkscape to draw a high-quality SVG and create my animation. So there's that. &lt;/p&gt;

&lt;p&gt;Below is a demo of the logo I made that day.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/xmUiPIarGSI"&gt;
&lt;/iframe&gt;
 &lt;/p&gt;
&lt;h3&gt;
  
  
  Creating Scalable Vector Graphics(SVG)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/SVG" rel="noopener noreferrer"&gt;Here&lt;/a&gt; is the MDN documentation for SVG if you're interested.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This isn't a "how to use Inkscape" guide. I will go over the steps I took to create the above example with a short demo.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Below is the (sped up) process for creating the SVG using Inkscape. It consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;creating a text object and selecting the desired font&lt;/li&gt;
&lt;li&gt;using the pen tool to draw Bézier curves and lines&lt;/li&gt;
&lt;li&gt;setting fill and stroke (note: I added fill by mistake in this demo so you'll see me add it and then remove it)&lt;/li&gt;
&lt;li&gt;editing paths by nodes to properly align stroke (I went quick for the purposes of this demo so excuse the spazzing on the H--luckily there's no sound)&lt;/li&gt;
&lt;li&gt;selecting object to path &lt;/li&gt;
&lt;li&gt;selecting clip -&amp;gt; set&lt;/li&gt;
&lt;li&gt;saving as optimized SVG&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/HVkaexpLpBg"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Rendering SVG and Animating with CSS
&lt;/h3&gt;

&lt;p&gt;I'm using React for this demo. I have a functional component that returns my SVG. I opened the file using VSCode and, in this case, simply copied the code into my component, removing the &lt;code&gt;&amp;lt;defs&amp;gt;&lt;/code&gt; tags and &lt;code&gt;&amp;lt;g&amp;gt;&lt;/code&gt; tags and their contents. I then added &lt;code&gt;className&lt;/code&gt; to the &lt;code&gt;svg&lt;/code&gt; tag for sizing and positioning, and to the final &lt;code&gt;path&lt;/code&gt; tag which handles the animation. So I'm left with something like this: &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%2Fi.imgur.com%2Fe7WaBHmh.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%2Fi.imgur.com%2Fe7WaBHmh.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And in my CSS file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.img {
  width: 100%;
  height: 250px;
  margin: auto;
}

.path {
  opacity: 0;
  stroke-dasharray: 4000;
  stroke-dashoffset: 5000; 
  animation: dash 4s linear forwards;
}


@keyframes dash {
  to {
    opacity: 1;
    stroke-dashoffset: 0;
    transition: transform 900ms linear;
      will-change: transform;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Final product: &lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/34Ahmtx8wdc"&gt;
&lt;/iframe&gt;
 &lt;/p&gt;

&lt;h3&gt;
  
  
  Welp, That's It
&lt;/h3&gt;

&lt;p&gt;As stated on my profile: [I love being a] perpetual learner and creator. Software development not only allows for that but demands it. I'm excited to get more practice and exposure to the world of CSS animation as I had a lot of fun making my first ones. &lt;/p&gt;

</description>
      <category>css</category>
      <category>animation</category>
    </item>
    <item>
      <title>Interactive Navigation in React SPA</title>
      <dc:creator>CiaraMaria</dc:creator>
      <pubDate>Mon, 21 Dec 2020 21:31:34 +0000</pubDate>
      <link>https://dev.to/mariaverse/interactive-navigation-in-react-spa-2ha9</link>
      <guid>https://dev.to/mariaverse/interactive-navigation-in-react-spa-2ha9</guid>
      <description>&lt;p&gt;I wanted a way to show a top navigation bar in the hero section and a hamburger drop-down navigation as a user scrolls down the rest of the page as part of my React single page application. &lt;/p&gt;

&lt;p&gt;After a lot of Googling along with some trial and error, I put together a few ideas to achieve my desired end result which looks like this: &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%2Fi.imgur.com%2F1OGO1wX.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%2Fi.imgur.com%2F1OGO1wX.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting Started
&lt;/h3&gt;

&lt;p&gt;I used: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/package/react-scroll" rel="noopener noreferrer"&gt;react-scroll&lt;/a&gt; &lt;code&gt;npm i react-scroll&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/package/react-burger-menu" rel="noopener noreferrer"&gt;react-burger-menu&lt;/a&gt; &lt;code&gt;npm i react-burger-menu&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Two separate components: one for top nav and one for the drop-down nav&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Setting Up Burger Menu
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/react-burger-menu" rel="noopener noreferrer"&gt;Instructions for burger set up and customization are here&lt;/a&gt;. Here is what my Sidenav.js and Sidenav.css files looked like after tweaking the default:&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, {Component} from 'react';
import './Sidenav.css';
import { slide as Menu } from 'react-burger-menu';
import {Link} from 'react-scroll';

class Sidenav extends Component {

  render () {

    return (
      &amp;lt;div className='sidenav'&amp;gt;
        &amp;lt;Menu width={ '15%' } &amp;gt;

          &amp;lt;Link className='nav-li' style={{ cursor: 
           "pointer"}} to="home" spy={true} smooth= 
           {true}&amp;gt;Home&amp;lt;/Link&amp;gt;

          &amp;lt;Link className='nav-li' style={{ cursor: 
           "pointer"}} to="about" spy={true} smooth= 
           {true}&amp;gt;About&amp;lt;/Link&amp;gt;

          &amp;lt;Link className='nav-li' style={{ cursor: 
           "pointer"}} to="work" spy={true} smooth= 
           {true}&amp;gt;Work&amp;lt;/Link&amp;gt;

          &amp;lt;Link className='nav-li' style={{ cursor: 
           "pointer"}} to="contact" spy={true} smooth= 
           {true}&amp;gt;Contact&amp;lt;/Link&amp;gt;

          &amp;lt;a className='nav-li' target='_blank' rel="noopener 
           noreferrer" href='linktoyourblog'&amp;gt;Blog&amp;lt;/a&amp;gt;

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

export default Sidenav;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Position and sizing of burger button */
.bm-burger-button {
  position: fixed;
  width: 30px;
  height: 25px;
  left: 10px;
  top: 36px;
}

/* Color/shape of burger icon bars */
.bm-burger-bars {
  background-color: #EFC6BA;
}


/* Position and sizing of clickable cross button */
.bm-cross-button {
  height: 24px;
  width: 24px;
}

/* Color/shape of close button cross */
.bm-cross {
  background: #222222;
}

/*
Sidebar wrapper styles
Note: Beware of modifying this element as it can break the animations - you should not need to touch it in most cases
*/
.bm-menu-wrap {
  position: fixed;
  height: 100%;
}

/* General sidebar styles */
.bm-menu {
  background: #FFFFFF;
  padding: 2.5em 0.5em 0;
  font-size: 1.15em;
}

/* Morph shape necessary with bubble or elastic */
.bm-morph-shape {
  fill: #FFFFFF;
}

/* Wrapper for item list */
.bm-item-list {
  color: #FFFFFF;
  padding: 0.8em;
}

/* Individual item */
.bm-item {
  display: inline-block;
}

/* Styling of overlay */
.bm-overlay {
  background: #FFFFFF;
}

/* Styling of links */
.nav-li {
  font-family: 'Playfair Display';
  color: #000000;
  outline: none;
  padding: 10%;
  text-decoration: underline;
  text-transform: uppercase;
}

/* Removes purple outline when clicked */
.nav-li:focus {
  outline: none;
}

.nav-li:hover {
  text-decoration: none;
  font-weight: bold;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Managing State
&lt;/h4&gt;

&lt;p&gt;At this point, the burger menu is working. There are additional state and event handlers available which are useful. By adding the following above &lt;code&gt;render()&lt;/code&gt;, the side menu will close when a link is clicked rather than having to click on the 'X':&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
    state = {
      menuOpen: false,
    }

    handleStateChange (state) {
      this.setState({menuOpen: state.isOpen})  
    }

    // closes menu on link click
    closeMenu () {
      this.setState({menuOpen: false})
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, I added props to the Menu and Link components in &lt;code&gt;return()&lt;/code&gt;. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Menu receives &lt;code&gt;isOpen={this.state.menuOpen} 
onStateChange={(state) =&amp;gt; this.handleStateChange(state)}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Link (and &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt;) receives &lt;code&gt;onClick={() =&amp;gt; this.closeMenu()}&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Menu width={ '15%' } isOpen={this.state.menuOpen} 
  onStateChange={(state) =&amp;gt; this.handleStateChange(state)}&amp;gt;

 &amp;lt;Link className='nav-li' style={{ cursor: "pointer"}} 
  to="home" spy={true} smooth={true} onClick={() =&amp;gt; 
  this.closeMenu()}&amp;gt;Home&amp;lt;/Link&amp;gt;

 &amp;lt;Link className='nav-li' style={{ cursor: "pointer"}} 
  to="about" spy={true} smooth={true} onClick={() =&amp;gt; 
  this.closeMenu()}&amp;gt;About&amp;lt;/Link&amp;gt;

 &amp;lt;Link className='nav-li' style={{ cursor: "pointer"}} 
  to="work" spy={true} smooth={true} onClick={() =&amp;gt; 
  this.closeMenu()}&amp;gt;Work&amp;lt;/Link&amp;gt;

 &amp;lt;Link className='nav-li' style={{ cursor: "pointer"}} 
  to="contact" spy={true} smooth={true} onClick={() =&amp;gt; 
  this.closeMenu()}&amp;gt;Contact&amp;lt;/Link&amp;gt;

 &amp;lt;a className='nav-li' target='_blank' rel="noopener 
  noreferrer" href='linktoyourblog' onClick={() =&amp;gt; 
  this.closeMenu()}&amp;gt;Blog&amp;lt;/a&amp;gt;

&amp;lt;/Menu&amp;gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright! The burger menu is fully functional now. &lt;/p&gt;

&lt;p&gt;So... how do we only make it visible as a user scrolls away from the landing view?&lt;/p&gt;

&lt;h3&gt;
  
  
  componentDidMount() and onscroll events
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/react-component.html#componentdidmount" rel="noopener noreferrer"&gt;componendDidMount()&lt;/a&gt; is invoked immediately after a component is mounted. As this happens we will watch for a scroll event to initiate a state change which changes the &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; opacity. &lt;/p&gt;

&lt;p&gt;In Sidenav.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; state = {
      menuOpen: false,
      opacity: '0'
    }

    // will show sidenav when scroll position is above 500
    componentDidMount() {
      if (typeof window !== 'undefined') {
        window.onscroll = () =&amp;gt; {
          let currentScrollPos = window.pageYOffset;
          let maxScroll = document.body.scrollHeight - 
          window.innerHeight;
          if (currentScrollPos &amp;lt; 500 &amp;amp;&amp;amp; currentScrollPos &amp;lt; 
           maxScroll) { 
            this.setState({ opacity: '0'})
            // to get position: console.log(currentScrollPos)
           } else {
            this.setState({ opacity: '1' })
          }
        }
      }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's happening here? I'm using state and inline CSS styling to control the visibility of the drop-down menu. I added opacity to our state and give it a default value of 0 so that it does not display on page load. In the function, I use &lt;code&gt;setState&lt;/code&gt; to change the opacity based on the current scroll position of the page. &lt;/p&gt;

&lt;p&gt;Let's go back to &lt;code&gt;return()&lt;/code&gt;. All of the content should be wrapped in a &lt;code&gt;&amp;lt;div className='sidenav&amp;gt;&lt;/code&gt;, to which we add inline styling:&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;div className='sidenav' style={{ opacity: `${this.state.opacity}`}}&amp;gt;

...

&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! Now the drop-down is only visible after a certain point on the page view. &lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion &amp;amp; Addressing Performance
&lt;/h3&gt;

&lt;p&gt;A somewhat hodgepodge interactive navigation design for SPA's. &lt;/p&gt;

&lt;p&gt;After doing more research on &lt;code&gt;componentDidMount()&lt;/code&gt; and &lt;code&gt;onscroll&lt;/code&gt; events, I noticed some concerns about performance issues using this type of approach. In its current usage, this isn't &lt;em&gt;completely&lt;/em&gt; killing my app's performance.&lt;/p&gt;

&lt;p&gt;Upon running a Lighthouse audit I get: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FaxtsYrF.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%2Fi.imgur.com%2FaxtsYrF.png"&gt;&lt;/a&gt;&lt;br&gt;
Although, that is a drop from the previous 97 on Performance and 100 on Best Practices. 👀&lt;/p&gt;

&lt;p&gt;That being said, I'd love to have a discussion on performance or perhaps a more efficient way to implement this interactive navigation that I'm not aware of-- so if you have something to teach me, drop a comment below! 💭&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Using PropTypes in React</title>
      <dc:creator>CiaraMaria</dc:creator>
      <pubDate>Fri, 11 Dec 2020 19:04:15 +0000</pubDate>
      <link>https://dev.to/mariaverse/using-proptypes-in-react-1glp</link>
      <guid>https://dev.to/mariaverse/using-proptypes-in-react-1glp</guid>
      <description>&lt;p&gt;In a previous post, I wrote about passing props in React. 👇 &lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;div class="ltag__link__content"&gt;
    &lt;div class="missing"&gt;
      &lt;h2&gt;Article No Longer Available&lt;/h2&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Today, I will discuss how we can improve the way we receive props. &lt;/p&gt;

&lt;h3&gt;
  
  
  What's PropTypes?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/facebook/prop-types"&gt;PropTypes&lt;/a&gt; is a package provided by the React team. It allows us to define the type of prop being passed down to a component and alerts us if an incorrect prop is used. When building a small application on your own this isn't really an issue, but when working on a team using PropTypes can ensure that if someone uses your component incorrectly during development, they will get a warning and fix the error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing PropTypes
&lt;/h3&gt;

&lt;p&gt;PropTypes is part of React and just requires installation. To use it in your React App simply run: &lt;code&gt;npm install --save prop-types&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up our example
&lt;/h3&gt;

&lt;p&gt;For our example, we will use a simple app that displays a person's name and age. We will be working with two files, &lt;code&gt;App.js&lt;/code&gt; and &lt;code&gt;Person.js&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Our App Component should 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 React, { Component } from 'react'
import './App.css';
import Person from './Person/Person';

class App extends Component {

  state = {
    people: [
      {id: 1, name: 'Marzbarz', age: 30},
      {id: 2, name: 'Rino', age: 34}
    ]
  }

  render() {

    let people = &amp;lt;div&amp;gt; {this.state.people.map((person, index) =&amp;gt; {
      console.log(person)
      return &amp;lt;Person key={index} name={person.name} age={person.age}/&amp;gt;
    })}
    &amp;lt;/div&amp;gt;

    return (
      &amp;lt;div className="App-header"&amp;gt;
        {people}
      &amp;lt;/div&amp;gt;
    );
  };
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's pretty much the basic layout provided when you &lt;code&gt;create-react-app&lt;/code&gt;, we have added state which holds the data for our people in an array of objects and has &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, and &lt;code&gt;age&lt;/code&gt;. Inside the &lt;code&gt;render()&lt;/code&gt; we are mapping over our array of people objects and passing props down to the Person Component. Then inside the &lt;code&gt;return()&lt;/code&gt; we use JSX to call our variable created for mapping. &lt;/p&gt;

&lt;p&gt;Our Person Component should 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 React from 'react';

const Person = (props) =&amp;gt; {
    return(
        &amp;lt;div&amp;gt;
            &amp;lt;p&amp;gt; Hi my name is {props.name} and I'm {props.age} years old.&amp;lt;/p&amp;gt;
        &amp;lt;/div&amp;gt;
    )
}

export default Person;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is simply taking in props and returning a &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; element with some text. &lt;/p&gt;

&lt;p&gt;If you open up your browser at this point you should see:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tGLIohuD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.imgur.com/OrPXaZH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tGLIohuD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.imgur.com/OrPXaZH.png" width="800" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Using PropsTypes
&lt;/h3&gt;

&lt;p&gt;We will be setting up PropTypes inside our &lt;code&gt;Person.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;import React from 'react';
import PropTypes from 'prop-types';

const Person = (props) =&amp;gt; {
    return(
        &amp;lt;div&amp;gt;
            &amp;lt;p&amp;gt; Hi my name is {props.name} and I'm {props.age} years old.&amp;lt;/p&amp;gt;
        &amp;lt;/div&amp;gt;
    )
}

Person.propTypes = {
    name: PropTypes.string,
    age: PropTypes.number
}

export default Person;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Take a look at the above code snippet. We &lt;code&gt;import PropTypes from 'prop-types'&lt;/code&gt; and after the component definition we access our component &lt;code&gt;Person&lt;/code&gt;, add a new property by adding a dot, and then &lt;code&gt;propTypes&lt;/code&gt;-- notice that the 'p' is lowercase here. Now we create a JavaScript object with key/value pairs where the keys are the prop names and the values are &lt;code&gt;PropTypes&lt;/code&gt;--notice this time the 'p' is uppercase--and the type of data. If you remember from our state in &lt;code&gt;App.js&lt;/code&gt;, the name prop was set to a string and the age prop was set to a number. There are multiple types of data available and you should see a suggestion dropdown like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1hVuW95G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.imgur.com/QfKutkI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1hVuW95G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.imgur.com/QfKutkI.png" width="800" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As it stands, nothing should change with our application. In order to see PropTypes in action let's make a small change to state inside &lt;code&gt;App.js&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;state = {
    people: [
      {id: 1, name: 'Marzbarz', age: '30'},
      {id: 2, name: 'Rino', age: '34'}
    ]
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's switch the age value from a number to a string as shown above. Now, this prop is being passed down a string but our PropTypes in &lt;code&gt;Person.js&lt;/code&gt; is still expecting a number. &lt;/p&gt;

&lt;p&gt;If we check our console in the browser there is now a warning message! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MSFJ40Ap--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.imgur.com/hLZDYE2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MSFJ40Ap--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.imgur.com/hLZDYE2.png" width="800" height="215"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Using PropTypes is especially helpful when working on teams or in a scenario where other developers are using your components and you want to be very clear on which props a component takes and what type of data is being passed.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Quick minimal contact form creation in React App</title>
      <dc:creator>CiaraMaria</dc:creator>
      <pubDate>Sun, 06 Dec 2020 00:41:48 +0000</pubDate>
      <link>https://dev.to/mariaverse/quick-minimal-contact-form-creation-in-react-app-50a2</link>
      <guid>https://dev.to/mariaverse/quick-minimal-contact-form-creation-in-react-app-50a2</guid>
      <description>&lt;p&gt;In my last 3 projects, I've needed to add a contact form. So I decided to write up a simple and speedy base layout for a form that can be customized easily. &lt;/p&gt;

&lt;p&gt;First our JSX which consists of our main &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;, a header, and a few &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; tags:&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;div className='contact'&amp;gt;
  &amp;lt;h2 className='contactus'&amp;gt;Contact Us&amp;lt;/h2&amp;gt;
    &amp;lt;input type="text" id="name" name="name" 
     placeholder="Name"/&amp;gt;
    &amp;lt;input type="text" id="email" name="email" 
     placeholder="Email"/&amp;gt;
    &amp;lt;textarea id="subject" name="subject" placeholder="Tell us 
     about you!" /&amp;gt;
    &amp;lt;input type="submit" value="Send"/&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, in our CSS file we will add some styling to the main &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;, our text input, and our submit input or button:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.contact {
  margin-top: 50px;
  margin-left: 400px;
  width: 500px;
  height: auto;
  padding: 10px;
  border: 8px solid transparent;
}

input[type=text], select, textarea {
  width: 90%; 
  padding: 12px; 
  border: 1px solid #262626; 
  box-sizing: border-box;
  margin-top: 5px; 
  margin-bottom: 5px; 
  resize: vertical; 
  font-size: medium;
  background-color: transparent;
}

input[type=submit] {
  background-color: transparent;
  border: none; 
  color: #262626;
  width: 100px;
  padding: 4px;
  cursor: pointer;
  font-size: medium;
  margin-left: 35%;
}

input[type=submit]:hover {
  color: #262626;
  transform: scale(1.2);
  transition: 500ms;
  border: 1px solid #262626;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--36lFvJGu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://i.imgur.com/PZl4qFO.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--36lFvJGu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://i.imgur.com/PZl4qFO.gif" width="600" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From here you can change up the styling to make it look how you want!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Additional uses for the React "children" prop</title>
      <dc:creator>CiaraMaria</dc:creator>
      <pubDate>Sat, 28 Nov 2020 21:30:03 +0000</pubDate>
      <link>https://dev.to/mariaverse/additional-uses-for-the-react-children-prop-8hm</link>
      <guid>https://dev.to/mariaverse/additional-uses-for-the-react-children-prop-8hm</guid>
      <description>&lt;p&gt;In the last post of my React series, I went over how to pass props including calling on the "children" prop. &lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;div class="ltag__link__content"&gt;
    &lt;div class="missing"&gt;
      &lt;h2&gt;Article No Longer Available&lt;/h2&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Today, I will dive a bit deeper into the "children" prop by demonstrating another usage: rendering adjacent JSX elements.&lt;/p&gt;

&lt;p&gt;React components must return a single JSX element (typically a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;). That element can have other elements inside of it, but you must only return one root JSX element. This means that an extra &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; gets rendered with each component along with everything inside of it.&lt;/p&gt;

&lt;p&gt;What if we don't want the extra element?&lt;/p&gt;

&lt;p&gt;We can achieve this by using &lt;a href="https://reactjs.org/docs/higher-order-components.html" rel="noopener noreferrer"&gt;Higher-Order Components&lt;/a&gt;. A higher-order component is a function that takes a component and returns a new component. HOC along with the "children" prop can be used where we want to have adjacent JSX elements without an extra DOM element being rendered.&lt;/p&gt;

&lt;h4&gt;
  
  
  Using the &lt;code&gt;props.children&lt;/code&gt; in HOC
&lt;/h4&gt;

&lt;p&gt;Picking up from the example in the preceding post, we have a component that renders a Person.&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';

const Person = (props) =&amp;gt; {

    return (
        &amp;lt;div&amp;gt;
            &amp;lt;p onClick={props.click}&amp;gt;I'm {props.name} and I am 
               {props.age} years old&amp;lt;/p&amp;gt;
            &amp;lt;p&amp;gt;{props.children}&amp;lt;/p&amp;gt;
            &amp;lt;input type="text" onChange={props.change} value= 
               {props.name} /&amp;gt; 
        &amp;lt;/div&amp;gt;   
    )
}

export default Person;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Currently, we have two &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; elements and an &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; wrapped inside of the &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;If we removed the enclosing &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; tags we would get the following error:&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%2Fi.imgur.com%2FtltGYjhl.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%2Fi.imgur.com%2FtltGYjhl.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's use HOC and &lt;code&gt;props.children&lt;/code&gt; to demonstrate an alternative way to render these adjacent elements. &lt;/p&gt;

&lt;p&gt;I created a folder &lt;code&gt;src/hoc&lt;/code&gt; and a file inside of it named &lt;code&gt;Aux.js&lt;/code&gt; which will hold our higher-order component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Aux = props =&amp;gt; props.children;

export default Aux;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The sole purpose of our new &lt;code&gt;Aux&lt;/code&gt; components is to return &lt;code&gt;props.children&lt;/code&gt; which is a special property that simply outputs whatever is entered between the opening and closing tags of a component. Refer to "Passing Props in React" linked at the start of this post for more on &lt;code&gt;props.children&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;We can now go back to our &lt;code&gt;Person&lt;/code&gt; component and make the following changes:&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 Aux from '../../../hoc/Aux';

const Person = (props) =&amp;gt; {

    return (
        &amp;lt;Aux&amp;gt;
            &amp;lt;p onClick={props.click}&amp;gt;I'm {props.name} and I am 
               {props.age} years old&amp;lt;/p&amp;gt;
            &amp;lt;p&amp;gt;{props.children}&amp;lt;/p&amp;gt;
            &amp;lt;input type="text" onChange={props.change} value= 
            {props.name} /&amp;gt; 
        &amp;lt;/Aux&amp;gt;
    )
}

export default Person;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have imported the &lt;code&gt;Aux&lt;/code&gt; component and replace the enclosing &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; element with opening and closing tags for &lt;code&gt;Aux&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;It is basically an empty wrapper utilizing the "children" property to render the JSX elements between the opening and closing tags of the &lt;code&gt;Aux&lt;/code&gt; component!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Side note: you may have noticed there is no React import in the &lt;code&gt;Aux&lt;/code&gt; component. This is because we're not using any JSX in it, so we're not implicitly using &lt;code&gt;React.createElement()&lt;/code&gt; and therefore it works without the import.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's it!&lt;/p&gt;

&lt;p&gt;I hope this quick detour into exploring other uses for the "children" prop was helpful. &lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Starter Guide to Big O Notation</title>
      <dc:creator>CiaraMaria</dc:creator>
      <pubDate>Sun, 22 Nov 2020 01:59:53 +0000</pubDate>
      <link>https://dev.to/mariaverse/starter-guide-to-big-o-notation-4kng</link>
      <guid>https://dev.to/mariaverse/starter-guide-to-big-o-notation-4kng</guid>
      <description>&lt;p&gt;As a developer without a Computer Science background, Big O Notation is one of those concepts that I've had to double down on in order to improve my algorithmic skills. In this post, I will cover some basics of Big O using JavaScript.&lt;/p&gt;

&lt;h4&gt;
  
  
  What is Big O Notation?
&lt;/h4&gt;

&lt;p&gt;Big O Notation is used to describe how long a function takes to run and allows us to talk formally about how the runtime grows as the inputs grow. It specifically describes the worst-case scenario in terms of runtime.&lt;/p&gt;

&lt;p&gt;Big O is expressed as O(&lt;em&gt;n&lt;/em&gt;) where &lt;em&gt;n&lt;/em&gt; is the size of the input.&lt;br&gt;
We will look at three specific expressions: O(1), O(&lt;em&gt;n&lt;/em&gt;), and O(&lt;em&gt;n²&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;When determining the amount of time it takes to run an algorithm, we have some helpful rules of thumb:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;When considering Big O, we only care about the broadest view. We're looking at what happens as &lt;em&gt;n&lt;/em&gt; gets ridiculously large. As this happens, the significant effect of adding 100 or multiplying by 5 decreases.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Constants don't matter:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;If we have O(5 &lt;em&gt;n&lt;/em&gt;) we simplify to O(&lt;em&gt;n&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;If we have O(42) we simplify to O(1)&lt;/li&gt;
&lt;li&gt;If we have O(10 &lt;em&gt;n²&lt;/em&gt;) we simplify to O(&lt;em&gt;n²&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  O(1)
&lt;/h4&gt;

&lt;p&gt;This expression means that a function takes a constant amount of runtime. Whether the input is 1 or 1000, as the input grows the time expended remains the same.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function logAtMostFive(n) {
  for (let i = 1; i &amp;lt;= Math.min(5, n); i++) {
    console.log(i)
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above function, regardless of what &lt;em&gt;n&lt;/em&gt; is the runtime will remain constant. So, if you run this function you'll notice that whether you pass in &lt;code&gt;5&lt;/code&gt; or &lt;code&gt;200&lt;/code&gt;, the log will only be &lt;code&gt;1, 2, 3, 4, 5&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As a result, we can say that the Big O Notation for this function is O(1). &lt;/p&gt;

&lt;h4&gt;
  
  
  O(&lt;em&gt;n&lt;/em&gt;)
&lt;/h4&gt;

&lt;p&gt;This expression means that it takes an amount of time linear with the size of &lt;em&gt;n&lt;/em&gt;. The runtime will increase in relation to the increase of the input.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function logAtLeastFive(n) {
  for (let i = 1; i &amp;lt;= Math.max(5, n); i++) {
    console.log(i)
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above function, we see the opposite effect as in the previous example. As &lt;em&gt;n&lt;/em&gt; increases, runtime increases in relation to &lt;em&gt;n&lt;/em&gt;. If you run this function, you'll notice that if you pass in &lt;code&gt;5&lt;/code&gt; the log will be &lt;code&gt;1, 2, 3, 4, 5&lt;/code&gt; however, if you pass in &lt;code&gt;200&lt;/code&gt; the log will be &lt;code&gt;1, 2, 3, 4, 5, 6, 7...200&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As a result, we can say that the Big O Notation for this function is O(&lt;em&gt;n&lt;/em&gt;). &lt;/p&gt;

&lt;h4&gt;
  
  
  O(&lt;em&gt;n²&lt;/em&gt;)
&lt;/h4&gt;

&lt;p&gt;This expression means that an algorithm's runtime is directly proportional to the square of &lt;em&gt;n&lt;/em&gt;. Time will exponentially increase in relation to the increase of the input. This is common in functions that involve nested iterations. Deeper nesting will result in O(&lt;em&gt;n³&lt;/em&gt;), O(&lt;em&gt;n⁴&lt;/em&gt;), etc.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function printAllPairs(n) {
  for (var i = 0; i &amp;lt;= n; i++) {
    for (var j = 0; j &amp;lt;= n; j++) {
      console.log(i, j);
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above function, as &lt;em&gt;n&lt;/em&gt; increases, the runtime increases exponentially. This can seriously inhibit performance as the size of &lt;em&gt;n&lt;/em&gt; gets ridiculously large as mentioned earlier.&lt;/p&gt;

&lt;p&gt;As a result, we can say that the Big O Notation for this function is O(&lt;em&gt;n²&lt;/em&gt;).&lt;/p&gt;

&lt;h4&gt;
  
  
  Conclusion
&lt;/h4&gt;

&lt;p&gt;Big O Notation allows us to describe the amount of time an algorithm will take to run. As applications grow and the amount or size of data being handled grows, the way our functions process that information becomes critical.&lt;/p&gt;

&lt;p&gt;Hopefully, you enjoyed this starter guide to Big O! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/26xBI73gWquCBBCDe/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/26xBI73gWquCBBCDe/giphy.gif" width="374" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>javascript</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Passing Props in React</title>
      <dc:creator>CiaraMaria</dc:creator>
      <pubDate>Sun, 15 Nov 2020 03:04:01 +0000</pubDate>
      <link>https://dev.to/mariaverse/passing-props-in-react-4cno</link>
      <guid>https://dev.to/mariaverse/passing-props-in-react-4cno</guid>
      <description>&lt;p&gt;How do you pass data from one component to another in a React application? Cue, Props!&lt;/p&gt;

&lt;h4&gt;
  
  
  What are props?
&lt;/h4&gt;

&lt;p&gt;"Props" is short for properties and can be used to pass dynamic data between components. Props at their core are plain JavaScript objects which hold attributes. React has a uni-directional data flow meaning props get passed from parent to child. &lt;/p&gt;

&lt;h4&gt;
  
  
  How do we pass props?
&lt;/h4&gt;

&lt;p&gt;We'll be building on our previous example from "Getting started with React." &lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;div class="ltag__link__content"&gt;
    &lt;div class="missing"&gt;
      &lt;h2&gt;Article No Longer Available&lt;/h2&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;





&lt;p&gt;We have our App and Person components. We will be passing data from App to Person in order to give our person a name. &lt;/p&gt;

&lt;p&gt;Our App.js file looks 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, { Component } from 'react'
import './App.css';
import Person from './Person/Person';

class App extends Component {

  render() {

    return (
      &amp;lt;div className="App-header"&amp;gt;
        &amp;lt;h1&amp;gt;I'm building a React app!&amp;lt;/h1&amp;gt;
        &amp;lt;Person /&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  };
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our Person.js file looks 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';

const Person = () =&amp;gt; {
    return(
        &amp;lt;div&amp;gt;
            &amp;lt;p&amp;gt; Hi my name is... &amp;lt;/p&amp;gt;
        &amp;lt;/div&amp;gt;
    )
}

export default Person;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is what the above code currently renders in the browser: &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%2Fi.imgur.com%2Fn70UjBi.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%2Fi.imgur.com%2Fn70UjBi.png" alt="react-example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's name our person! We can do this by hardcoding the name in Person.js, sure. However, we want to make our component flexible, configurable, and dynamic. &lt;/p&gt;

&lt;p&gt;In App.js, we can pass attributes to our Person component like so:&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;Person name="Marzbarz" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have created a keyword, in this case &lt;code&gt;name&lt;/code&gt;, and assigned it a string. &lt;/p&gt;

&lt;p&gt;If you try to run this, you'll notice that nothing has changed and that is because we need to add some things to our Person.js file in order for it to handle this new data.&lt;/p&gt;

&lt;p&gt;In Person.js we will receive one argument in our function which is passed by default by React. This is props and while you don't have to name it 'props,' it is conventional and recommended.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Person = (props) =&amp;gt; {
    return(
        &amp;lt;div&amp;gt;
            &amp;lt;p&amp;gt; Hi my name is {props.name} &amp;lt;/p&amp;gt;
        &amp;lt;/div&amp;gt;
    )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we are passing props as an argument and returning "Hi my name is &lt;code&gt;{props.name}&lt;/code&gt;" with "name" being the keyword that we assigned to our property in App.js. This syntax is &lt;a href="https://reactjs.org/docs/introducing-jsx.html" rel="noopener noreferrer"&gt;JSX&lt;/a&gt;. The curly braces tell the JSX parser that the syntax should be interpreted as JavaScript even though it is surrounded by HTML.&lt;/p&gt;

&lt;p&gt;Now the browser should look like this: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FCbXKWNa.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%2Fi.imgur.com%2FCbXKWNa.png" alt="react-example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Awesome! We have successfully passed props from one component to another!&lt;/p&gt;
&lt;h4&gt;
  
  
  Understanding the "children" prop
&lt;/h4&gt;

&lt;p&gt;There is a special prop called "children" which is a reserved word. "Children" refers to any elements including plain text that are written in between the opening and closing tags of a child component.&lt;/p&gt;

&lt;p&gt;For example, say we make this change to our App.js file:&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;Person name={'Marzbarz'}&amp;gt; and I love to code! &amp;lt;/Person&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As it stands, nothing will look different in the browser. But if we now go into Person.js and add to our return:&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;p&amp;gt; Hi my name is {props.name} 
    {props.children}
&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll now see: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FpbOiAZA.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%2Fi.imgur.com%2FpbOiAZA.png" alt="react-example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nice! We have dynamically passed data in two ways!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Of course, as our applications grow and become more complex we may need to introduce state which will affect how we pass props. In my next series entry, I will dive into understanding and using state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Conclusion
&lt;/h4&gt;

&lt;p&gt;We looked at what props are, practiced passing props, and accessing the reserved "children" prop.&lt;/p&gt;

&lt;p&gt;I hope you found this guide helpful! As I continue on in my React series, I welcome feedback, discussion, and suggestions for concepts you'd like to see me tackle. &lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>props</category>
    </item>
    <item>
      <title>Lottie Animations for React</title>
      <dc:creator>CiaraMaria</dc:creator>
      <pubDate>Sun, 08 Nov 2020 02:16:55 +0000</pubDate>
      <link>https://dev.to/mariaverse/lottie-animations-for-react-1c9l</link>
      <guid>https://dev.to/mariaverse/lottie-animations-for-react-1c9l</guid>
      <description>&lt;p&gt;Who doesn't love animations? They're fun, engaging, and versatile. In this post, I will introduce &lt;a href="https://lottiefiles.com/featured" rel="noopener noreferrer"&gt;Lottie&lt;/a&gt; animations and how to implement them into your React website. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://imgur.com/6JX6Hb1" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2F6JX6Hb1.gif" title=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a Lottie?
&lt;/h3&gt;

&lt;p&gt;A Lottie is a JSON-based animation file format that enables designers to ship animations on any platform. They are small files that work on any device and can scale up or down without pixelation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Integrating Lottie into React App
&lt;/h3&gt;

&lt;h5&gt;
  
  
  Step 1: install the library
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;npm install --save react-lottie&lt;/code&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Step 2: select an animation from &lt;a href="https://lottiefiles.com/featured" rel="noopener noreferrer"&gt;LottieFiles&lt;/a&gt;
&lt;/h5&gt;

&lt;p&gt;You can download it as a JSON or use the Lottie Animation URL.&lt;br&gt;
Personally, I prefer to download and store it in a folder in my application. &lt;br&gt;
Here's an example of the file structure:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FWvwBH1sm.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%2Fi.imgur.com%2FWvwBH1sm.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  Step 3: create a Lottie.js file in src
&lt;/h5&gt;

&lt;p&gt;The code inside that file should 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 React from "react";
import Lottie from "react-lottie";

export default function LottieAnimation({ lotti, width, height }) {
  const defaultOptions = {
    loop: true,
    autoplay: true,
    animationData: lotti,
    rendererSettings: {
      preserveAspectRatio: "xMidYMid slice",
    },
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;Lottie options={defaultOptions} height={height} width={width} /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Step 4: import your Lottie into the component you want to return the animation:
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import LottieAnimation from '../Lottie';
import home from '../Animation/dev.json';

const Example = () =&amp;gt; {

    return ( 
         &amp;lt;div className='example'&amp;gt; 
           &amp;lt;LottieAnimation lotti={home} height={300} width={300} /&amp;gt;
        &amp;lt;/div&amp;gt;
    )
}

export default Example; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: This example implies that you then add &lt;code&gt;&amp;lt;Example /&amp;gt;&lt;/code&gt; into render in App.js &lt;/p&gt;
&lt;/blockquote&gt;


 

&lt;p&gt;That's it! Your beautiful Lottie animation should now render to your site!&lt;/p&gt;

&lt;p&gt;As always, feedback and discussion is welcome. 💁&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Complex Forms in Rails</title>
      <dc:creator>CiaraMaria</dc:creator>
      <pubDate>Sun, 01 Nov 2020 02:34:50 +0000</pubDate>
      <link>https://dev.to/mariaverse/complex-forms-in-rails-2lkh</link>
      <guid>https://dev.to/mariaverse/complex-forms-in-rails-2lkh</guid>
      <description>&lt;p&gt;Rails offers all kinds of intuitive solutions for programmers, one of which is form helpers. In this post, I'll be breaking down the steps for creating a nested form - which is basically a form within a form. Formception.&lt;/p&gt;

&lt;p&gt;When creating my first Rails project, I found myself searching for guidance on creating this type of form. While there are many sources that show you how to do this with a &lt;code&gt;has_many&lt;/code&gt; association, I struggled to find examples for &lt;code&gt;belongs_to&lt;/code&gt;. So, we will be working with a complex form for a joins table.&lt;/p&gt;

&lt;p&gt;We're building a mood diary. We have two models to work with regarding our nested form: Entry and Emotion.&lt;/p&gt;

&lt;p&gt;Entry is our joins table and so it's association is &lt;code&gt;belongs_to :emotion&lt;/code&gt;, while Emotion &lt;code&gt;has_many :entries&lt;/code&gt;.&lt;br&gt;
When imagining the functionality of our form, we can conclude that we want to have the ability to create our Entry and either assign it an existing emotion or create a new one.&lt;/p&gt;

&lt;p&gt;First, we want to use &lt;code&gt;accepts_nested_attributes_for&lt;/code&gt; class method in our Entry model. This method defines an attributes writer for the specified association, in this case, Emotion.&lt;br&gt;
Going along with the Rails penchant for convention over configuration, a good rule of thumb to remember when setting up nested forms is:&lt;br&gt;
If the association 'belongs to' a singular model (e.g., emotion) we will maintain the singular form of reference. If the association 'has many' plural model&lt;a href="//e.g.,%20entries"&gt;s&lt;/a&gt; we will maintain the plural form of reference throughout our code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HRIOGc75--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.imgur.com/DbqPMHsl.png%3F1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HRIOGc75--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.imgur.com/DbqPMHsl.png%3F1" width="640" height="85"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we will need to modify our declared permitted parameters to accommodate the nested attributes. Following our example, we will add this to our EntriesController. In the code below I have added a key for association attributes, where the association is the relationship you want to 'nest' into your form. Put simply, it's the one you added to &lt;code&gt;accepts_nested_attributes_for&lt;/code&gt;. This key holds the specified association's attributes that we want passed in.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fK5CaDi3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.imgur.com/UwBbMV3h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fK5CaDi3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.imgur.com/UwBbMV3h.png" width="800" height="113"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: In this example, &lt;code&gt;:emotion_id&lt;/code&gt; is included outside of &lt;code&gt;emotion_attributes:&lt;/code&gt; - As shown in the Rails guide we can also use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def entry_params
    params.require(:entry).permit(:content, emotion_attributes: [
         :id, :name
    ])
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we are ready to create our form!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t4PggELS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.imgur.com/4NW9Cvyh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t4PggELS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.imgur.com/4NW9Cvyh.png" width="800" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The magic happens within the &lt;code&gt;fields_for&lt;/code&gt; tag which, when associated with &lt;code&gt;accepts_nested_attributes&lt;/code&gt;, renders a block once for every element of the association. We then use the &lt;code&gt;build_association&lt;/code&gt; method which instantiates the object from the passed attributes and sets a link through the foreign key.&lt;/p&gt;

&lt;p&gt;Note: Pay close attention to &lt;code&gt;build_association&lt;/code&gt; method. Not to be confused with &lt;code&gt;collection.build&lt;/code&gt;, which instantiates an object from the passed attributes and creates a foreign key. The latter is a method made accessible when declaring a &lt;code&gt;has_many&lt;/code&gt; association.&lt;/p&gt;

&lt;p&gt;Our final product (plus a bit of CSS) looks like this, with the option to select or create an emotion:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7U9KOG7S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.imgur.com/noG5u0g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7U9KOG7S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.imgur.com/noG5u0g.png" width="772" height="754"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
