View index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
View PageTwo.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import { useGlobalState } from "./GlobalStateProvider"; | |
const PageTwo = () => { | |
const { state } = useGlobalState(); | |
return ( | |
<div> | |
<h1>State from PageOne:</h1> |
View PageOne.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
View App.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
View GlobalStateProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, |
View useGlobalState.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useGlobalState = () => { | |
const context = useContext(GlobalStateContext); | |
if (!context) { | |
throw new Error("useGlobalState must be used within a GlobalStateContext"); | |
} | |
return context; | |
}; |
View GlobalStateProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const GlobalStateProvider = ({ | |
children, | |
value = {} as GlobalStateInterface, | |
}: { | |
children: React.ReactNode; | |
value?: Partial<GlobalStateInterface>; | |
}) => { | |
const [state, setState] = useState(value); | |
return ( | |
<GlobalStateContext.Provider value={{ state, setState }}> |
View GlobalStateContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const GlobalStateContext = createContext({ | |
state: {} as Partial<GlobalStateInterface>, | |
setState: {} as Dispatch<SetStateAction<Partial<GlobalStateInterface>>>, | |
}); |
View GlobalStateInterface.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface GlobalStateInterface { | |
firstname: string; | |
lastname: string; | |
age: string; | |
} |
NewerOlder