Skip to content

Instantly share code, notes, and snippets.

View jamiehaywood's full-sized avatar

Jamie Haywood jamiehaywood

View GitHub Profile
@jamiehaywood
jamiehaywood / axios.js
Created May 13, 2020 13:52
Axios Key Rotation
import axios from "axios";
import { getHeaders } from "./auth/getHeaders";
import { updateTokens } from "./auth/updateTokens";
// https://github.com/axios/axios/issues/1383
const customAxios = axios.create({
baseURL: process.env.REACT_APP_CAPTURAPI,
});
@jamiehaywood
jamiehaywood / SVGButton.tsx
Created May 13, 2020 13:57
Reusable SVG base TypeScript button. It includes types to make it easy to consume.
import React from "react";
import "./Button.scss";
import * as SVG from "../../images";
import { Link } from "react-router-dom";
interface ButtonProps
extends React.DetailedHTMLProps<
React.ButtonHTMLAttributes<HTMLButtonElement>,
HTMLButtonElement
> {
export interface GlobalStateInterface {
firstname: string;
lastname: string;
age: string;
}
const GlobalStateContext = createContext({
state: {} as Partial<GlobalStateInterface>,
setState: {} as Dispatch<SetStateAction<Partial<GlobalStateInterface>>>,
});
const GlobalStateProvider = ({
children,
value = {} as GlobalStateInterface,
}: {
children: React.ReactNode;
value?: Partial<GlobalStateInterface>;
}) => {
const [state, setState] = useState(value);
return (
<GlobalStateContext.Provider value={{ state, setState }}>
const useGlobalState = () => {
const context = useContext(GlobalStateContext);
if (!context) {
throw new Error("useGlobalState must be used within a GlobalStateContext");
}
return context;
};
import React, { createContext, useState, useContext, Dispatch, SetStateAction } from "react";
export interface GlobalStateInterface {
firstname: string;
lastname: string;
age: string;
}
const GlobalStateContext = createContext({
state: {} as Partial<GlobalStateInterface>,
import React from "react";
import { Route, Switch } from "react-router-dom";
import { GlobalStateProvider } from "./GlobalStateProvider";
import PageOne from "./PageOne";
import PageTwo from "./PageTwo";
function App() {
return (
<Switch>
import React from "react";
import { useHistory } from "react-router-dom";
import { useForm } from "react-hook-form";
import { useGlobalState, GlobalStateInterface } from "./GlobalStateProvider";
const PageOne = () => {
const history = useHistory();
const { handleSubmit, register } = useForm();
const { setState } = useGlobalState();
import React from "react";
import { useGlobalState } from "./GlobalStateProvider";
const PageTwo = () => {
const { state } = useGlobalState();
return (
<div>
<h1>State from PageOne:</h1>