Skip to content

Instantly share code, notes, and snippets.

View coryhouse's full-sized avatar

Cory House coryhouse

View GitHub Profile
import React, { useState, useEffect } from "react";
import { getUsers } from "./services/userService";
export default function CentralDemo() {
const [users, setUsers] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
getUsers()
.then(json => {
export function getUsers() {
return fetch(`${process.env.REACT_APP_API_BASE_URL}users`).then(response =>
response.json()
);
}
import React, { useState, useEffect } from "react";
export default function InlineDemo() {
const [users, setUsers] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
fetch(`${process.env.REACT_APP_API_BASE_URL}users`)
.then(response => {
@coryhouse
coryhouse / useIsTabbing.js
Last active June 26, 2020 19:02
React Hook to monitor if user is tabbing
import { useState, useEffect } from "react";
/* Track whether user is interacting via keyboard.
This is used to determine if we should show focus rings.
Focus rings should only display on focus when isTabbing is true.
*/
export default function useIsTabbing() {
const [isTabbing, setIsTabbing] = useState(false);
useEffect(function listenForMouseClicksAndKeydowns() {
@coryhouse
coryhouse / InlineAlert.tsx
Last active April 14, 2021 07:34
Only allow certain child element types in React component
import React, { Suspense } from "react";
import "./InlineAlert.scss";
import cx from "classnames";
import * as ReactIs from "react-is";
const allowedChildren = ["string", "span", "em", "b", "i", "strong"];
type InlineAlertProps = {
/** Message to display */
children: React.ReactNode;
@coryhouse
coryhouse / PromptOnUnload.jsx
Last active January 7, 2022 11:53
Prompt user when they navigate away
import React, { useEffect } from "react";
import PropTypes from "prop-types";
import { Prompt } from "react-router-dom";
// Prompt the user onbeforeunload or when navigating away by clicking a link
export default function PromptOnUnload({ when, message }) {
const enabled = when;
useEffect(() => {
if (!enabled) return;
const handleBeforeUnload = (event) => {
@coryhouse
coryhouse / BankHolidaysProvider.js
Last active February 12, 2020 13:44
React context provider that caches requests to avoid duplicate calls by each datepicker
/**
This provides bank holidays and a function to request bank holidays.
This is currently only used by the datepicker.
This provider assures each year's bank holidays are only requested once for a given app.
Place this provider at the app's entry point so that all child components can consume it.
**/
import React, { useState, useRef } from "react";
import PropTypes from "prop-types";
import BankHolidaysContext from "./BankHolidaysContext";
import { convertToDateObjects } from "../../../utils/dateUtils";
@coryhouse
coryhouse / Layout.jsx
Created January 24, 2020 11:07
Example App Layout React component
import React, {
isValidElement,
Suspense,
useState,
useRef,
useEffect
} from "react";
import PropTypes from "prop-types";
import { Route } from "react-router-dom";
import H1 from "reusable/lib/H1";
@coryhouse
coryhouse / useAlert.js
Last active January 24, 2020 11:00
Hook for easily creating an alert
// Hook to make consuming Alert context more convenient
import { useContext } from "react";
import AlertContext from "../components/AlertContext";
import { ALERT_TYPES } from "reusable/lib/Alert";
import { useHistory } from "react-router";
export default function useAlert() {
const history = useHistory();
const { setAlerts } = useContext(AlertContext);
@coryhouse
coryhouse / useSafeState.js
Created January 23, 2020 01:35
React useSafeState hook - Only set state if mounted
import { useEffect, useRef, useState } from "react";
/**
* Provides the functionality of React.useState() with the only difference that
* it sets a state only if its parent component is still mounted, aka "safe setState".
*/
export default function useSafeState(initialState) {
const mountedRef = useRef(false);
const [state, setState] = useState(initialState);