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"; | |
export const createGenericContext = <T extends unknown>() => { | |
// Create a context with a generic parameter or undefined | |
const genericContext = React.createContext<T | undefined>(undefined); | |
// Check if the value provided to the context is defined or throw an error | |
const useGenericContext = () => { | |
const contextIsDefined = React.useContext(genericContext); | |
if (!contextIsDefined) { |
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 { useSidebarContext } from "./useSidebarContext"; | |
export const ChildComponent: React.FC = () => { | |
const [isOpen, setIsOpen] = useSidebarContext(); | |
return ( | |
<> | |
<p>Sidebar is: {isOpen ? "Open" : "Closed"}</p> | |
<button onClick={() => setIsOpen(!isOpen)}>Toggle sidebar</button> |
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 * as React from "react"; | |
import { render } from "react-dom"; | |
import { SidebarProvider, useSidebarContext } from "./useSidebarContext"; | |
import { ChildComponent } from "./childComponent"; | |
export const App = () => { | |
const [isOpen] = useSidebarContext(); | |
return ( | |
<div> |
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 * as React from "react"; | |
import { render } from "react-dom"; | |
import { SidebarProvider, useSidebarContext } from "./useSidebarContext"; | |
import { ChildComponent } from "./childComponent"; | |
export const App = () => { | |
const [isOpen] = useSidebarContext(); | |
return ( | |
<div> |
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
// Add a new generic type T | |
const filterArrayByValue = <T>( | |
items: T[], | |
propertyName: string, | |
valueToFilter: string | |
): T[] => { | |
return items.filter(item =>item[propertyName] === valueToFilter); | |
}; |
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
// Add a new generic type P | |
const filterArrayByValue = <T, P extends keyof T>( | |
items: T[], | |
propertyName: P, | |
valueToFilter: string | |
): T[] => { | |
return items.filter(item =>item[propertyName] === valueToFilter); | |
}; |