Skip to content

Instantly share code, notes, and snippets.

Avatar

Brady Clifford bradyclifford

View GitHub Profile
@bradyclifford
bradyclifford / useSafeState.js
Last active Nov 5, 2020
Safe Unmounted React Hooks
View useSafeState.js
import { useState, useRef, useEffect, useCallback } from 'react';
/*
* useSafeState prevents state from being updated if a component has been
* unmounted during an async process, thus preventing React from throwing errors
* around it.
*
* Usage is just like regular useState:
* const [myState, setMyState] = useSafeState("Initial state");
*/
@bradyclifford
bradyclifford / useFetch.js
Created Nov 4, 2020
Axios Cancelation Hook
View useFetch.js
import axios from 'axios';
import { useEffect } from 'react';
function useEffectAsync(callbackAsync, useEffectDeps) {
var source = axios.CancelToken.source();
useEffect(()=> {
callbackAsync(source.token)
.catch(error => {
if(!axios.isCancel(error)) {
throw error;
View SessionProvider.js
import React, { createContext, useContext, useState } from '~/session/react';
import Session from 'sessionModule';
import { resetSessionRequest } from 'session-client';
const SessionContext = createContext(Session.default());
SessionContext.displayName = 'Session';
function useSession(initialSession) {
const [session, setSession] = useState(initialSession);
@bradyclifford
bradyclifford / Test.js
Created Feb 2, 2020
JavaScript Date Assessment
View Test.js
/* Assessment:
* Native date creation -> Non-ISO will create date in current timezone
* Native date creation -> ISO will create date in GMT timezone
- When parsing "literal" ISO, parseISO & parseJSON are same - identical to native
* When parsing "short" ISO, parseISO converts ISO string yyyy/mm/dd to local timezone (adds time offset) unlike native
* formatISO should be used for converstion to date string yyyy/mm/dd? What happens when it is converted back into a DateTime? Assume UTC?
* DatePicker, push in a parseISO date or ISO string, outputs a date type with HST offset
*
*/
describe('Date: Timezones', () => {
@bradyclifford
bradyclifford / App.js
Last active Jun 27, 2019
Polling in React
View App.js
import React, { useState, useEffect } from 'react';
import './App.css';
import { Route, Switch } from 'react-router';
import { Container, Breadcrumb, BreadcrumbItem, Button } from 'reactstrap';
import useFetch from './util/useFetch';
import { Home } from './routes/Home';
import { Buyer } from './routes/Buyer';
import { Seller } from './routes/Seller';
import { Demo } from './routes/Demo';
import { Scoreboard } from './components/Scoreboard';
View ChildProps.js
import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
const UserAvatar = ({ user, size }) => (
<img
className={`user-avatar ${size || ""}`}
alt="user avatar"
src={user.avatar}
/>
View ServiceCollectionExtensions
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace Web.Extensions
{
public static class ServiceCollectionExtensions
@bradyclifford
bradyclifford / removeAllLocalStaleBranches.sh
Created Feb 12, 2018
Removes all local branches of which are stale
View removeAllLocalStaleBranches.sh
git fetch -p && for branch in `git branch -vv --no-color | grep ': gone]' | awk '{print $1}'`; do git branch -D $branch;