Skip to content

Instantly share code, notes, and snippets.

View jamiehaywood's full-sized avatar

Jamie Haywood jamiehaywood

View GitHub Profile
@jamiehaywood
jamiehaywood / index.html
Last active October 10, 2021 22:38
Unique Array Based on Property #jsbench #jsperf (https://jsbench.github.io/#c0a4801fcf85b656704ab8cef9a4fc89) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Unique Array Based on Property #jsbench #jsperf</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
import React from "react";
import { useGlobalState } from "./GlobalStateProvider";
const PageTwo = () => {
const { state } = useGlobalState();
return (
<div>
<h1>State from PageOne:</h1>
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 { Route, Switch } from "react-router-dom";
import { GlobalStateProvider } from "./GlobalStateProvider";
import PageOne from "./PageOne";
import PageTwo from "./PageTwo";
function App() {
return (
<Switch>
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>,
const useGlobalState = () => {
const context = useContext(GlobalStateContext);
if (!context) {
throw new Error("useGlobalState must be used within a GlobalStateContext");
}
return context;
};
const GlobalStateProvider = ({
children,
value = {} as GlobalStateInterface,
}: {
children: React.ReactNode;
value?: Partial<GlobalStateInterface>;
}) => {
const [state, setState] = useState(value);
return (
<GlobalStateContext.Provider value={{ state, setState }}>
const GlobalStateContext = createContext({
state: {} as Partial<GlobalStateInterface>,
setState: {} as Dispatch<SetStateAction<Partial<GlobalStateInterface>>>,
});
export interface GlobalStateInterface {
firstname: string;
lastname: string;
age: string;
}
@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
> {