Skip to content

Instantly share code, notes, and snippets.

View cassidoo's full-sized avatar
⌨️
sleepy

Cassidy Williams cassidoo

⌨️
sleepy
View GitHub Profile
////////////// HTML //////////////
<!-- item will be appened to this layout -->
<div id="log" class="sl__chat__layout">
</div>
<!-- chat item -->
<script type="text/template" id="chatlist_item">
<div data-from="{from}" data-id="{messageId}">
<span class="meta" style="color: {color}">
@cassidoo
cassidoo / routingExample.js
Last active June 23, 2020 23:34
A React Router example
// This is the main layout of the whole website for selling some products
export default function PrimaryLayout() {
return (
<>
<Header />
<main>
<Switch>
<Route path="/" exact>
<Home />
</Route>
export default function usePromise(api) {
const [state, dispatch] = useReducer(
(state, action) => {
switch (action.type) {
case 'LOADING':
return { ...state, loading: true }
case 'RESOLVED':
return { ...state, loading: false, response: action.response, error: null }
case 'ERROR':
return { ...state, loading: false, response: null, error: action.error }
@cassidoo
cassidoo / wizeline.js
Created May 22, 2020 00:28
Wizeline webinar on hooks
import React, { useState, useEffect, useReducer, useMemo } from 'react'
import ReactDOM from 'react-dom'
// let text = 'hellllooooo'
// I AM REACT
// let domTable = {}
// let element = Counter // state = 0
// commit(element, domTable)
@cassidoo
cassidoo / useLocal.js
Created April 23, 2020 23:42
An example of a local storage hook
import { useEffect } from "react";
import { useAppReducer } from "../AppContext";
export default function useLocal() {
const dispatch = useAppReducer();
useEffect(() => {
dispatch({
type: "GET_FROM_LOCAL_STATE",
somePieceOfState: localStorage.getItem("some_state") || "defaultvalue",
@cassidoo
cassidoo / ref.jsx
Last active April 23, 2020 23:43
forwardRef example
export const SomeButton = forwardRef(
(
{ children, onSelect, ...props },
forwardedRef
) => {
return (
<button
{...props}
onClick={onSelect}
ref={forwardedRef}
@cassidoo
cassidoo / useMedia.jsx
Last active October 27, 2022 16:13
An example of checking on a media query with React Hooks
function useMedia(query) {
const [matches, setMatches] = useState(window.matchMedia(query).matches)
useEffect(() => {
const media = window.matchMedia(query)
if (media.matches !== matches) {
setMatches(media.matches)
}
const listener = () => {
setMatches(media.matches)
@cassidoo
cassidoo / cities.js
Created April 9, 2020 00:20
Name a city. It's in there.
const cities = [
{
city: "Abbeville",
state: "Louisiana",
},
{
city: "Aberdeen",
state: "Maryland",
},
{
import time
print "..."
time.sleep(1)
print "..."
print "..."
print "..."
print "..."
time.sleep(1)
print "..."
@cassidoo
cassidoo / customWhitespaceMatcher.js
Last active February 21, 2023 20:12
A custom matcher for when you want to compare strings in Jest and ignore whitespace
// Modified version of this Stack Overflow response:
// https://stackoverflow.com/a/48459005/1950503
// (Removes ramda dependency, adds .trim() to string replacer, adds it to Jest global var instead of exporting)
// To use:
// Put `<rootDir>/path/to/customWhitespaceMatcher.js` in your Jest config under setupFiles
// Call it in your tests like this:
// expect(
// customMatchers.whitespaceMatcher(receivedResult, expectedResult).pass
// ).toBeTruthy();