Skip to content

Instantly share code, notes, and snippets.

View itsmacr8's full-sized avatar

ITS MAC itsmacr8

View GitHub Profile
@itsmacr8
itsmacr8 / airtable.ts
Created September 5, 2024 18:40
FetchNextPage not working properly.
import Airtable, { Base } from 'airtable';
import { DatabaseRecord } from "../../types/DatabaseRecord.interface";
class AirTable {
private apiKey: string = import.meta.env.VITE_PWK;
private apiBase: string = import.meta.env.VITE_PWB;
public base: Base = this.getBase(this.apiKey, this.apiBase);
private dbRecords: DatabaseRecord[] = [];
private fetchNext!: () => void;
@itsmacr8
itsmacr8 / higher_order_component.js
Created October 13, 2023 15:14
How to use higher-order component basic example.
// Higher-order component
const withSubscription = (WrapperComponent, selectData) => {
return (props) => {
const [data, setData] = useState([]);
useEffect(() => {
const handleChange = () => {
const newData = selectData(DataSource, props);
setData(newData);
}
@itsmacr8
itsmacr8 / useRef_hook.js
Created October 12, 2023 15:23
How to use useRef hook basic example.
import { useRef } from 'react';
function App() {
const formInputRef = useRef(null)
const focusInput = () => {
formInputRef.current.focus();
}
return (
@itsmacr8
itsmacr8 / useReducer_hook.js
Created October 12, 2023 15:14
How to use useReducer hook basic example.
import { useReducer } from 'react';
const reducer = (state, action) => {
if(action.type === 'buy_ingredients') return {money: state.money - 10}
else if(action.type === 'sell_a_meal') return {money: state.money + 10}
return state;
}
function App() {
const initialState = {money: 100};
@itsmacr8
itsmacr8 / useEffect_hook.js
Last active October 11, 2023 17:11
Basic example of how to use react useEffect hook.
import React from "react";
function App(){
const [toggle, setToggle] = React.useEffect(false);
const handleToggle = () => {
setToggle(!toggle);
}
React.useEffect(() => {
@itsmacr8
itsmacr8 / controlled _element.js
Created October 10, 2023 17:12
How to create a control element in React.
function App(){
const [name, setName] = useState('')
const handleSubmit = (e) => {
e.preventDefault();
setName('')
console.log('Form submitted successfully');
}
const handleChange = (e) => {
@itsmacr8
itsmacr8 / assets.js
Created October 9, 2023 17:23
How to add assets in react applications.
import cat from './assets/images/cat.jpg';
function showAnimal(){
return (
<div>
// Method 1 (name reference asset)
<img src={cat} alt="A picture of a cat" loading="lazy" title="A picture of a cat"/>
// Method 2 (path reference asset)
<img src={require('./assets/images/cat.jpg')} alt="A picture of a cat" loading="lazy" title="A picture of a cat"/>
</div>
// If you use method 2 then you don't have to use the import statement.
@itsmacr8
itsmacr8 / App.js
Created October 9, 2023 16:59
How to use react router library for navigation.
import {Routes, Route, Link} from 'react-router-dom'
import Homepage from './Homepage';
import AboutMe from './AboutMe';
function App() {
return(
<div>
<nav>
<Link to="/">Homepage</Link>
<Link to="/about-me">About Me</Link>
@itsmacr8
itsmacr8 / Counter.js
Created October 8, 2023 16:56
This is a react app that track daily meal tracking with react context API
import {useMealsListContext} from '../providers/MealsProvider';
const Counter = () => {
const {meals} = useMealsListContext();
return (
<div>
<h3>Number of meals today: {meals.length}</h3>
</div>
);
}
@itsmacr8
itsmacr8 / dynamic_form.html
Created September 30, 2023 17:22
How to use the date, range, and drop-down options form elements.
<form class="form" method="post">
<div class="form-group">
<label for="booking-date">Booking date</label>
<input type="date" id="booking-date" name="booking-date" required>
</div>
<div class="form-group">
<label for="booking-people">Number of people</label>
<input type="range" id="booking-people" name="booking-people" min="1" max="10" value="4" oninput="this.nextElementSibling.value = this.value" required>
</div>
<div class="form-group">