Skip to content

Instantly share code, notes, and snippets.

View ChaseH88's full-sized avatar

Chase Harrison ChaseH88

View GitHub Profile
@ChaseH88
ChaseH88 / ReactComponent.tsx
Created April 27, 2021 19:55
Dynamic ReactComponent class to inject components into an existing DOM tree. An example use case would be to use this class when you are working with an app that isn't React first. NOTE: I also added Redux to this example to show how you can import your own libraries. This will be used on a page-by-page basis and the Redux store will not persist.
import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux'
import { store } from 'Config/store'
interface ReactComponentOptions {
domID: string
[key: string]: any
}
import axios from 'axios';
/**
* This hook makes use of the axios dependency.
* @param path the endpoint in which you wish to make a request to
* @param method the request method.. can be either 'get' or 'post'.
* @param data an object containing the data/payload
* @returns
*/
const useAxios = async (
@ChaseH88
ChaseH88 / React Shape Component - TypeScript
Created April 6, 2021 23:50
Quick component I put together that gives you some shapes you can use in the UI.
import React, { FC } from 'react';
interface ShapeInterface {
type: 'rectangle' | 'circle' | 'triangle',
backgroundColor?: string
style?: { [key: string]: any }
}
const Shape: FC<ShapeInterface> = ({ type, backgroundColor = '#252525', style }): JSX.Element => {
@ChaseH88
ChaseH88 / Use Render Delay Hook for React
Created April 5, 2021 04:50
A hook that gives you a boolean that you can use to delay rendering of your component.
import { useEffect, useState } from 'react';
/**
* Adds a render delay variable that can be used on components
* @param timeout
* @param dependency
* @example const visible = useRenderDelay(open, 2500);
*/
const useRenderDelay = (dependency: boolean, timeout = 1000): boolean => {
import React, { FC, lazy, Suspense, useState } from 'react';
// Code Spliting with dynamic imports
const MenuContainer = lazy(() =>
import('./MenuContainer')
.then(({ MenuContainer }) => ({ default: MenuContainer })),
);
const Navigation: FC = (): JSX.Element => {
@ChaseH88
ChaseH88 / Async For Each
Created March 24, 2021 23:36
Handle asynchronous events within a loop.
/**
* Handle async looping
* @param {any[]} array
* @param {function} callback
*/
async asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
@ChaseH88
ChaseH88 / MongoDB-Mongoose Class
Created March 21, 2021 12:21
MongoDB / Mongoose Class - A class that is setup and ready to go that allows you to connect to a particular MongoDB database instance.
import mongoose from 'mongoose';
interface DatabaseInterface {
start(): void
}
class Database implements DatabaseInterface {
private connection: string = '';
private username: string = 'YOUR_USERNAME';
import { Request, Response, RequestHandler, NextFunction } from 'express';
export const asyncHandler = (
fn: (req: Request, res: Response, next: NextFunction) => void
): RequestHandler => (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
@ChaseH88
ChaseH88 / useKeyPress.ts
Created January 10, 2021 22:50
This is a React hook that attaches keyup and keydown handlers to the window to detect whether or not a particular key code has been pressed.
import { useState, useEffect } from "react";
type KeyCode = { keyCode: number };
/**
* Attaches keyup and keydown handlers to the window to detect
* whether or not a particular key code has been pressed.
* @param targetKey The key code
* @returns {boolean}
* @example const isPressed = useKeyPress(13);
/**
* Extract specific files from HTMLWebpackPlugin
* https://webpack.js.org/plugins/html-webpack-plugin/
*/
<% function extractHash(pattern){ %>
<% return Object.values(htmlWebpackPlugin.files.js).filter(function(val) { %>
<% return val.match(pattern) %>
<% }); %>
<% } %>