Categories
Vue

Vue 3 — Props

Vue 3 is the up and coming version of Vue front end framework.

It builds on the popularity and ease of use of Vue 2.

In this article, we’ll look at how to use props with Vue 3.

Props

Components can take props so that we can pass data from the parent component to it.

We can add props by defining the as an array of strings.

For example, we can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>App</title>
    <script src="https://unpkg.com/vue@next"></script>
  </head>
  <body>
    <div id="app">
      <blog-post title="hello world"></blog-post>
    </div>
    <script>
      const app = Vue.createApp({}); 
      app.component("blog-post", {
        props: ["title"],
        template: `<h1>{{title}}</h1>`
      }); 
      app.mount("#app");
    </script>
  </body>
</html>

We created a blog-post component with the props property to define which props we accept.

The array has the names of the props as strings.

Then we can pass in the props as we did with title .

Props are just attributes that can have any value we want.

Prop Types

To make passing props easier, we can check the data type of the prop.

This way, we’ll get an error if we pass in some value that we don’t expect.

For example, we can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>App</title>
    <script src="https://unpkg.com/vue@next"></script>
  </head>
  <body>
    <div id="app">
      <blog-post title="hello world"></blog-post>
    </div>
    <script>
      const app = Vue.createApp({}); 
      app.component("blog-post", {
        props: {
          title: String
        },
        template: `<h1>{{title}}</h1>`
      }); 
      app.mount("#app");
    </script>
  </body>
</html>

to set the title prop type to a string.

We can pass in any other constructor to set the valid type of a prop, like:

props: {
  title: String,
  likes: Number,
  date: Date,
  commentIds: Array,
  author: Object,
  callback: Function,
  contactsPromise: Promise
}

This also serves as good documentation of ur component.

Passing Static or Dynamic Props

Static props can be passed in with v-bind .

We did with the title prop in the previous example.

We have:

<blog-post title="hello world"></blog-post>

which has the title prop and it’s a static string.

If we want to pass in anything else, then we need to use v-bind or : for short.

For example, we can write:

<blog-post :title="post.title"></blog-post>

to pass in the title property of the post object.

We can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>App</title>
    <script src="https://unpkg.com/vue@next"></script>
  </head>
  <body>
    <div id="app">
      <blog-post v-for="post of posts" :title="post.title"></blog-post>
    </div>
    <script>
      const app = Vue.createApp({
        data() {
          return {
            posts: [
              { title: "hello world", author: "james" },
              { title: "good news", author: "james" },
              { title: "bad news", author: "james" }
            ]
          };
        }
      }); 

      app.component("blog-post", {
        props: {
          title: String
        },
        template: `<h1>{{title}}</h1>`
      }); 
      app.mount("#app");
    </script>
  </body>
</html>

to pass the title property of post to the title prop.

We can also pass in an expression.

For instance, we can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>App</title>
    <script src="https://unpkg.com/vue@next"></script>
  </head>
  <body>
    <div id="app">
      <blog-post
        v-for="post of posts"
        :title="`${post.title} by ${post.author}`"
      ></blog-post>
    </div>
    <script>
      const app = Vue.createApp({
        data() {
          return {
            posts: [
              { title: "hello world", author: "james" },
              { title: "good news", author: "mary" },
              { title: "bad news", author: "jane" }
            ]
          };
        }
      }); 

      app.component("blog-post", {
        props: {
          title: String
        },
        template: `<h1>{{title}}</h1>`
      }); 
      app.mount("#app");
    </script>
  </body>
</html>

We created a string from the title and author properties and pass that in as the title prop’s value.

Conclusion

We can register props to let us pass values to child components.

Also, we can validate the data type with constructors as the values of the prop property.

Categories
Useful APIs

Useful Free APIs — More Business APIs

In the software development world, practice makes perfect. Therefore, we should find as many ways to practice programming as possible. With free public APIs, we can practice programming by creating apps that use those APIs.

In this article, we’ll look at some practice project ideas that can use some of those APIs.

Google Analytics

The Google Analytics API lets us configure and analyze web site analytics data programmatically.

There are many APIs under this API.

The Core Reporting API lets us a query for dimensions and metrics to create customized reports.

Embed API lets us create and embed dashboards on 3rd party websites.

Multi-Channel Funnels Reporting API lets us query traffic source paths that lead to the user’s goal conversion.

Real-Time Reporting API lets us report on activity occurring on our properties.

Metadata API lets us access the list of API dimensions and metrics and their attributes.

Management API lets us view and manage accounts, properties, views, filters, uploads, permissions, and more.

User Deletion API lets us delete all data in a Google Analytics property associated with a user ID.

The docs are available at https://developers.google.com/analytics/.

MailboxValidator

The MailboxValidator API lets us validate email addresses to improve deliverability.

The docs are available at https://www.mailboxvalidator.com/api-single-validation.

We can validate one address at a time, check for disposable email addresses, and check if an email address is from a free email provider.

An API key is required to access this API.

Client libraries are available for PHP, Java, VB.NET, C#, and Python.

mailgun API

The mailgun API is an email service for developers.

The docs are available at https://documentation.mailgun.com/en/latest/.

We can use it to send and receive emails, and track events.

Emails can be sent with various protocols.

They can also be queued and sent in batches.

Deliveries can also be scheduled with this API.

We can also use it to track message opens, clicks, unsubscribe, spam complaints, failures, deliveries, and more.

Some features are only available when we pay for the API.

An API key is required to access this API.

markerapi

The markerapi lets us search for trademarks.

We can search up to 1000 times a month for free.

To make more search requests, we’ve to pay up to 100 USD a month.

The API docs are at https://markerapi.com/.

An API key is required to access this API.

Ticksel

The Ticksel API lets us track our websites.

The docs are at https://www.ticksel.com/.

The free API lets us track 5000 page views a month.

We can track unlimited websites with the free plan.

12 months of data retention is also available.

10 user or session tags can be added for tracking.

All requests are made via SSL.

Trello

Trello is a task management software that we can use to track tasks easily in our projects.

Projects are tracked on boards with cards.

The API docs are at https://developer.atlassian.com/cloud/trello/.

We can expand it with add-ons with power-ups.

API authentication is done via OAuth.

The REST API lets us read and change action, boards, checklist, members, notifications, and reaction data.

Each of these entities has many fields available including badges, labels, URLs, subscribers, and much more.

Conclusion

We can do many things with Google, Trello, and other APIs.

They let us send emails, track tasks, and visitors, and much more.

Categories
Useful APIs

Useful Free APIs — Business Data

In the software development world, practice makes perfect. Therefore, we should find as many ways to practice programming as possible. With free public APIs, we can practice programming by creating apps that use those APIs.

In this article, we’ll look at some practice project ideas that can use some of those APIs.

Charity Search

The Charity Search API lets us search for nonprofit charity data.

The API is docs are located at http://charityapi.orghunter.com/charity-api-list.

We can get data like city, state, latitude, longitude, name, category, and more with this API.

Also, it provides us with financial data.

There’s also a premium version of this API with more data like rents, sales, revenue, assets, and more.

An API key is required to access this API.

Clearbit Logo API

The Logo API lets us access company logo data.

The API docs are located at https://clearbit.com/docs#logo-api.

Rate limiting is imposed on this API.

600 requests a minute is allowed with this API.

We can use this API to look up company info, person info, and more in addition to logos.

Logo data returned include image data and more.

An API key is required to access the log API.

Domainsdb.info

We can search for domain info with this API.

The docs for this API is at https://api.domainsdb.info/v1/.

For example, we can look for domain update history, deleted domains, added domains, and more.

Top-level domain statics are also available with this API.

No authentication is required to access this API.

Freelancer

The Freelancer API lets us get freelancer data and create, bid, and manage projects.

We can also use it to order services.

The API docs are located at https://developers.freelancer.com/docs.

OAuth is required to access this API.

Other kinds of information including contests, polls, comments, messages, timezones, offers, and more are also available.

Android and Python SDKs are available so that we can access this API more easily on those platforms.

Gmail

The Gmail API provides us with a flexible, RESTful API to let us control Gmail accounts programmatically.

OAuth is required to access this API.

Lots of things can be read and changed with this API, including user data, messages, labels, settings, and more.

There’re client libraries available for the browser, Go, Java, .Net, Node.js, PHP, Python, and Ruby.

So accessing the API should be easy.

The docs are available at https://developers.google.com/gmail/api.

Conclusion

We can find and manipulate business data with these useful APIs.

Categories
React

Animate with the react-spring Library — useSprings and useTrail Hooks

With the react-spring library, we can render animations in our React app easily.

In this article, we’ll take a look at how to get started with react-spring.

useSprings

The useSprings hook lets us create multiple springs each with its own config.

For example, we can write:

import React from "react";
import { animated, useSprings } from "react-spring";

const items = [
  { text: "foo", opacity: 0.3, id: 1 },
  { text: "bar", opacity: 0.6, id: 2 },
  { text: "baz", opacity: 0.9, id: 3 }
];

export default function App() {
  const springs = useSprings(
    items.length,
    items.map((item) => ({ opacity: item.opacity, from: { opacity: 0 } }))
  );

  return (
    <>
      {springs.map((props, i) => (
        <animated.div style={props} key={i}>
          {items[i].text}
        </animated.div>
      ))}
    </>
  );
}

to create the animation styles for each item with the useSprings hook.

The first argument is the number of style objects to create.

The 2nd argument is an array with the styles we want to apply.

Then in the JSX, we apply the styles by passing in the props to the style prop.

We can also control our animation with the set and stop functions.

For example, we can write:

import React from "react";
import { animated, useSprings } from "react-spring";

const items = [
  { text: "foo", opacity: 0.3, id: 1 },
  { text: "bar", opacity: 0.6, id: 2 },
  { text: "baz", opacity: 0.9, id: 3 }
];

export default function App() {
  const [springs, set, stop] = useSprings(items.length, (index) => ({
    opacity: items[index].opacity,
    from: { opacity: 0 }
  }));

  return (
    <>
      <button onClick={() => set((index) => ({ opacity: 1 }))}>start</button>
      <button onClick={() => stop()}>stop</button>
      {springs.map((props, i) => (
        <animated.div style={props} key={i}>
          {items[i].text}
        </animated.div>
      ))}
    </>
  );
}

We have the same items array.

And we use the useSpring hook with a function as the 2nd argument instead of an array of styles.

Calling it with a function as the 2nd argument will give us the set and stop functions in addition to springs ,

The set function lets us run the animation with the given style.

And the stop function lets us stop the animation.

Then we display the animated divs the same way.

useTrail

The useTrail lets us create multiple springs with a single config.

For example, we can write:

import React from "react";
import { animated, useTrail } from "react-spring";

const items = [
  { text: "foo", id: 1 },
  { text: "bar", id: 2 },
  { text: "baz", id: 3 }
];

export default function App() {
  const trail = useTrail(items.length, { opacity: 1, from: { opacity: 0 } });

  return (
    <>
      {trail.map((props, i) => (
        <animated.div style={props} key={i}>
          {items[i].text}
        </animated.div>
      ))}
    </>
  );
}

The first argument is the number of animation style objects to create.

And the 2nd argument is the animation style to create.

Then we can apply them as we do with the useSprings hook.

Conclusion

We can create animations for a group of components with the useSprings and useTrail hooks.

Categories
React

Animate with the react-spring Library — useSpring Hook

With the react-spring library, we can render animations in our React app easily.

In this article, we’ll take a look at how to get started with react-spring.

useSpring

We can use the useSpring hook to add our animation.

For example, we can write:

import React, { useEffect, useState } from "react";
import { useSpring, animated } from "react-spring";

export default function App() {
  const [props, set, stop] = useSpring(() => ({ opacity: 1 }));
  const [toggle, setToggle] = useState();

  useEffect(() => {
    set({ opacity: toggle ? 1 : 0 });
  }, [toggle]);

  return (
    <>
      <button onClick={() => setToggle(!toggle)}>start</button>
      <button onClick={() => stop()}>stop</button>
      <animated.div style={props}>i will fade</animated.div>
    </>
  );
}

We pass in callback into the useSpring hook to create our animation.

We also have the toggle state to let us toggle the opacity with the set function.

So when we click start, we’ll see the fading of the text toggled.

And we have the stop function to stop the animation.

We can also use the useSpring by passing in an object.

For example, we can write:

import React from "react";
import { useSpring, animated } from "react-spring";

export default function App() {
  const props = useSpring({
    from: { opacity: 0 },
    to: { opacity: 1, color: "red" }
  });

  return (
    <>
      <animated.div style={props}>i will fade</animated.div>
    </>
  );
}

We animate from the styles with the from property to the styles in the to property.

We can shorten this to:

import React from "react";
import { useSpring, animated } from "react-spring";

export default function App() {
  const props = useSpring({
    from: { opacity: 0 },
    opacity: 1,
    color: "red"
  });

  return (
    <>
      <animated.div style={props}>i will fade</animated.div>
    </>
  );
}

Async Chains and Scripts

We can animate asynchronously with the next function.

For instance, we can write:

import React from "react";
import { useSpring, animated } from "react-spring";

export default function App() {
  const props = useSpring({
    to: async (next, cancel) => {
      await next({ opacity: 1, color: "#ffaaee" });
      await next({ opacity: 0, color: "rgb(14,26,19)" });
    },
    from: { opacity: 0, color: "red" }
  });

  return (
    <>
      <animated.div style={props}>i will fade</animated.div>
    </>
  );
}

The to method is an async function that has the next and cancel functions as parameters.

next lets us set the styles we want to apply in our animation.

And cancel lets us cancel the animation.

from is still an object with the initial styles of our div.

Also, we can create the to property with an array by writing:

import React from "react";
import { useSpring, animated } from "react-spring";

export default function App() {
  const props = useSpring({
    to: [
      { opacity: 1, color: "#ffaaee" },
      { opacity: 0, color: "rgb(14,26,19)" }
    ],
    from: { opacity: 0, color: "red" }
  });

  return (
    <>
      <animated.div style={props}>i will fade</animated.div>
    </>
  );
}

The array has objects with the styles we want to apply.

Conclusion

The useSpring hook lets us create animations easy for a single element.