Categories
React Projects

Create a Random Joke App with Vue 3 and JavaScript

React is an easy to use JavaScript framework that lets us create front end apps.

In this article, we’ll look at how to create a random joke app with React and JavaScript.

Create the Project

We can create the React project with Create React App.

To install it, we run:

npx create-react-app random-joke

with NPM to create our React project.

Create the Rando Joke App

To create the random joke app, we write:

import { useEffect, useState } from "react";

export default function App() {
  const [joke, setJoke] = useState("");

  const getJoke = async () => {
    const res = await fetch("https://api.chucknorris.io/jokes/random");
    const { value } = await res.json();
    setJoke(value);
  };

  useEffect(() => {
    getJoke();
  }, []);

  return (
    <div className="App">
      <p>{joke}</p>
      <button onClick={getJoke}>get new joke</button>
    </div>
  );
}

We have the joke state, which is a string state created by useState .

Then we add the getJoke function to make an HTTP request to the Chuck Norris jokes API with fetch .

We make a GET request and call res.json() to return a promise with the data object.

Then we call setJoke to set the joke value.

We call getJoke with the useEffect callback to load the joke when we load the component.

The callback only runs when we mount it because we pass in an empty array into the 2nd component.

Then we render the joke string.

And then we add a button that calls getJoke when we click it.

Conclusion

We can create a random joke app easily with React and JavaScript.

Categories
React Projects

Create a Flashcard App with React and JavaScript

React is an easy to use JavaScript framework that lets us create front end apps.

In this article, we’ll look at how to create a flashcard app with React and JavaScript.

Create the Project

We can create the React project with Create React App.

To install it, we run:

npx create-react-app flashcard

with NPM to create our React project.

We need the uuid to let us create unique IDs for our flashcard items easily.

To add it, we run:

npm i uuidv4

Create the Flashcard App

To create the flashcard app, we write:

import { useState } from "react";
import { v4 as uuidv4 } from "uuid";

export default function App() {
  const [item, setItem] = useState({});
  const [items, setItems] = useState([]);

  const add = (e) => {
    e.preventDefault();
    const { question, answer } = item;
    const formValid = question && answer;
    if (!formValid) {
      return;
    }
    setItems((items) => [
      ...items,
      {
        id: uuidv4(),
        ...item
      }
    ]);
  };

  const deleteItem = (index) => {
    setItems((items) => items.filter((_, i) => i !== index));
  };

  return (
    <div className="App">
      <form onSubmit={add}>
        <div>
          <label>question</label>
          <input
            value={item.question}
            onChange={(e) =>
              setItem((item) => ({ ...item, question: e.target.value }))
            }
          />
        </div>
        <div>
          <label>answer</label>
          <input
            value={item.answer}
            onChange={(e) =>
              setItem((item) => ({ ...item, answer: e.target.value }))
            }
          />
        </div>
        <button type="submit">submit</button>
      </form>
      {items.map((item, index) => {
        return (
          <div key={item.id}>
            <b>question</b>
            <p>{item.question}</p>
            <b>answer</b>
            <p>{item.answer}</p>
            <button onClick={() => deleteItem(index)}>delete</button>
          </div>
        );
      })}
    </div>
  );
}

We have the item and items state created with the useState hook.

Then we have the add function to add an item to items .

In the function, we call e.preventDefault() to do client-side form submission.

Then we check if question and answer is set.

If they are, then we call setItems to add an item to the items array with the callback.

The callback gets the existing items value and returns a new array with the new item appended to it.

The deleteItem function calls setItems with a callback that returns the items array without the entry with the given index .

In the form, we have the onSubmit prop set to add so that add runs when we click on the submit button.

The inputs have the value and onChange props so that we bind the inputted value to the properties.

We pass in a callback to setItem to set the properties to e.target.value , which has the inputted value.

Below that, we render the items into elements.

We need to set the key prop to a unique ID so that React can identify each entry.

It also has a button that calls deleteItem with index when we click on it.

Conclusion

We can create a flashcard app with React and JavaScript easily.

Categories
React Projects

Create a Budget Tracker App with React and JavaScript

React is an easy to use JavaScript framework that lets us create front end apps.

In this article, we’ll look at how to create a budget tracker app with React and JavaScript.

Create the Project

We can create the React project with Create React App.

To install it, we run:

npx create-react-app budget-tracker

with NPM to create our React project.

We need the uuid to let us create unique IDs for our budget items easily.

To add it, we run:

npm i uuidv4

Create the Grocery List App

To create the grocery list app, we write:

import { useMemo, useState } from "react";
import { v4 as uuidv4 } from "uuid";

export default function App() {
  const [budget, setBudget] = useState("");
  const [expense, setExpense] = useState({});
  const [expenses, setExpenses] = useState([]);

  const add = (e) => {
    e.preventDefault();
    const { cost, description } = expense;
    const formValid = +budget > 0 && +cost > 0 && description;
    if (!formValid) {
      return;
    }
    setExpenses((ex) => [
      ...ex,
      {
        id: uuidv4(),
        ...expense
      }
    ]);
  };

  const remove = (index) => {
    setExpenses((expenses) => expenses.filter((_, i) => i !== index));
  };

  const remainingBudget = useMemo(() => {
    const totalExpenses = expenses
      .map(({ cost }) => +cost)
      .reduce((a, b) => +a + +b, 0);
    return budget - totalExpenses;
  }, [budget, expenses]);

  return (
    <div className="App">
      <div>
        <form>
          <fieldset>
            <label>budget</label>
            <input value={budget} onChange={(e) => setBudget(e.target.value)} />
          </fieldset>
        </form>

        <form onSubmit={add}>
          <h1>add expensse</h1>
          <fieldset>
            <label>description</label>
            <input
              value={expense.description}
              onChange={(e) =>
                setExpense((ex) => ({ ...ex, description: e.target.value }))
              }
            />
          </fieldset>

          <fieldset>
            <label>cost</label>
            <input
              value={expense.cost}
              onChange={(e) =>
                setExpense((ex) => ({ ...ex, cost: e.target.value }))
              }
            />
          </fieldset>
          <button type="submit">add expense</button>
        </form>

        <p>remaining budget: ${remainingBudget}</p>

        {expenses.map((ex, index) => {
          return (
            <div key={ex.id}>
              {ex.description} - ${ex.cost}
              <button onClick={() => remove(index)}>remove</button>
            </div>
          );
        })}
      </div>
    </div>
  );
}

We have the budget , expense , and expenses states.

budget has the budget number.

expense has the expense item.

expenses has the expense items.

Then add the add method that lets us add an expense item.

In it, we call e.preventDefault() to let us to client-side submission.

Then we check if the expense properties are valid with the formValid variable.

If it’s true , then we call setExpenses with a callback to return an expenses array with the new item appended to it.

In the remove method, we call setExpenses with a callback that returns the expenses array without the item with the given index .

Next, we add the remainingBudget state that we create with useMemo .

In the callback, we add up all the cost values from the expenses array together to get totalExpenses .

Then we subtract budget by totalExpenses.

We watch budget and expenses for changes as indicated by the array in the 2nd argument.

In the return statement, we have a form to let us enter the budget value.

In the 2nd form, we can enter values for the expense properties.

We set the value and onChange props so that we can set the properties.

To set a property value with the input, we use spread to spread the existing values in the return object, then we add the property we want to change set to e.target.value at the end.

Below that, we display the remainingBudget value.

And below that, we display the expenses items as divs.

The button inside calls remove with the index to remove an item when we click it.

Conclusion

We can create a simple budget app with React and JavaScript.

Categories
React Projects

Create a Budget Tracker App with React and JavaScript

React is an easy to use JavaScript framework that lets us create front end apps.

In this article, we’ll look at how to create a budget tracker app with React and JavaScript.

Create the Project

We can create the React project with Create React App.

To install it, we run:

npx create-react-app budget-tracker

with NPM to create our React project.

We need the uuid to let us create unique IDs for our to-do items easily.

To add it, we run:

npm i uuidv4

Create the Grocery List App

To create the grocery list app, we write:

import { useMemo, useState } from "react";
import { v4 as uuidv4 } from "uuid";

export default function App() {
  const [budget, setBudget] = useState("");
  const [expense, setExpense] = useState({});
  const [expenses, setExpenses] = useState([]);

  const add = (e) => {
    e.preventDefault();
    const { cost, description } = expense;
    const formValid = +budget > 0 && +cost > 0 && description;
    if (!formValid) {
      return;
    }
    setExpenses((ex) => [
      ...ex,
      {
        id: uuidv4(),
        ...expense
      }
    ]);
  };

  const remove = (index) => {
    setExpenses((expenses) => expenses.filter((_, i) => i !== index));
  };

  const remainingBudget = useMemo(() => {
    const totalExpenses = expenses
      .map(({ cost }) => +cost)
      .reduce((a, b) => +a + +b, 0);
    return budget - totalExpenses;
  }, [budget, expenses]);

  return (
    <div className="App">
      <div>
        <form>
          <fieldset>
            <label>budget</label>
            <input value={budget} onChange={(e) => setBudget(e.target.value)} />
          </fieldset>
        </form>

        <form onSubmit={add}>
          <h1>add expensse</h1>
          <fieldset>
            <label>description</label>
            <input
              value={expense.description}
              onChange={(e) =>
                setExpense((ex) => ({ ...ex, description: e.target.value }))
              }
            />
          </fieldset>

          <fieldset>
            <label>cost</label>
            <input
              value={expense.cost}
              onChange={(e) =>
                setExpense((ex) => ({ ...ex, cost: e.target.value }))
              }
            />
          </fieldset>
          <button type="submit">add expense</button>
        </form>

        <p>remaining budget: ${remainingBudget}</p>

        {expenses.map((ex, index) => {
          return (
            <div key={ex.id}>
              {ex.description} - ${ex.cost}
              <button onClick={() => remove(index)}>remove</button>
            </div>
          );
        })}
      </div>
    </div>
  );
}

We have the budget , expense , and expenses states.

budget has the budget number.

expense has the expense item.

expenses has the expense items.

Then add the add method that lets us add an expense item.

In it, we call e.preventDefault() to let us to client-side submission.

Then we check if the expense properties are valid with the formValid variable.

If it’s true , then we call setExpenses with a callback to return an expenses array with the new item appended to it.

In the remove method, we call setExpenses with a callback that returns the expenses array without the item with the given index .

Next, we add the remainingBudget state that we create with useMemo .

In the callback, we add up all the cost values from the expenses array together to get totalExpenses .

Then we subtract budget by totalExpenses.

We watch budget and expenses for changes as indicated by the array in the 2nd argument.

In the return statement, we have a form to let us enter the budget value.

In the 2nd form, we can enter values for the expense properties.

We set the value and onChange props so that we can set the properties.

To set a property value with the input, we use spread to spread the existing values in the return object, then we add the property we want to change set to e.target.value at the end.

Below that, we display the remainingBudget value.

And below that, we display the expenses items as divs.

The button inside calls remove with the index to remove an item when we click it.

Conclusion

We can create a simple budget app with React and JavaScript.

Categories
React Projects

Create a Grocery List App with React and JavaScript

React is an easy to use JavaScript framework that lets us create front end apps.

In this article, we’ll look at how to create a grocery list app with React and JavaScript.

Create the Project

We can create the React project with Create React App.

To install it, we run:

npx create-react-app grocery-list

with NPM to create our React project.

We need the uuid to let us create unique IDs for our to-do items easily.

To add it, we run:

npm i uuidv4

Create the Grocery List App

To create the grocery list app, we write:

import { useState } from "react";
import { v4 as uuidv4 } from "uuid";

export default function App() {
  const [item, setItem] = useState("");
  const [items, setItems] = useState([]);
  const add = (e) => {
    e.preventDefault();
    if (!item) {
      return;
    }
    setItems((items) => [
      ...items,
      {
        id: uuidv4(),
        item
      }
    ]);
  };

  const remove = (index) => {
    setItems((items) => items.filter((_, i) => i !== index));
  };

  return (
    <div className="App">
      <form onSubmit={add}>
        <fieldset>
          <label>item</label>
          <input value={item} onChange={(e) => setItem(e.target.value)} />
        </fieldset>
        <button type="submit">add item</button>
      </form>
      {items.map((item, index) => {
        return (
          <div key={item.id}>
            <p>{item.item}</p>
            <button onClick={() => remove(index)}>remove</button>
          </div>
        );
      })}
    </div>
  );
}

We have the item and items state created with the useState hook.

item has the item text.

items has the items array.

Then we add the add method which lets us add an item to the items array.

Inside it, we call e.preventDefault() to do client-side submission.

Then we check if item is set.

If it is, then we call setItems with a callback to return the items array with the new item added to the end.

The remove method calls setItems with a callback to return a items array without the item with the given index .

Then in the return statement, we have a form with the onSubmit prop that calls add .

An input element has the vbalue and onChange props to make it a controlled input.

We set the item value to e.target.value .

Then we render the items array into divs with the item.item text inside.

The button calls remove when we click it.

Conclusion

We can create a grocery list app easily with React and JavaScript.