This file contains hidden or 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 OPTIONS = [ | |
{ value: 'rcube', label: 'Rubix Cube' }, | |
{ value: 'headphones', label: 'Bose Headphones' }, | |
{ value: 'mic', label: 'Mic' }, | |
] as const; | |
const App = () => ( | |
<Dropdown<typeof OPTIONS[number]> // generic explicitly passed | |
options={OPTIONS} | |
onSelection={(opt) => { |
This file contains hidden or 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
type Option = { value: string; label: string }; | |
interface DropdownProps<TOption> { | |
options: ReadonlyArray<TOption>; | |
onSelection: (option: TOption) => void; | |
} | |
function Dropdown<TOption extends Option> ({ options, onSelection }: DropdownProps<TOption>) { | |
return ( |
This file contains hidden or 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
interface DropdownProps<TOption> { | |
options: ReadonlyArray<TOption>; | |
onSelection: (option: TOption) => void; | |
} | |
function Dropdown<TOption> ({ options, onSelection }: DropdownProps<TOption>) { | |
return ( | |
// implementation skipped for article clarity | |
) |
This file contains hidden or 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'; | |
const SpicyStateCtx = React.createContext(null); | |
const SpicyApiCtx = React.createContext(null); | |
// custom provider | |
const SpicyProvider = ({ children }) => { | |
const [spice, setSpice] = React.useState("pepper"); | |
const api = React.useMemo( |
This file contains hidden or 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'; | |
const SpicyCtx = React.createContext(null); | |
// custom provider | |
export const SpicyProvider = ({ children }) => { | |
const [spice, setSpice] = React.useState("scary"); | |
const api = { | |
gingerSpice: () => setSpice("ginger"), |
This file contains hidden or 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'; | |
const SpicyCtx = React.createContext(null); | |
// custom hook | |
export const useSpicyState = () => { | |
const ctx = React.useContext(SpicyCtx); | |
if (!ctx) { | |
throw new Error("useSpicyState must be used within the SpicyProvider") |
This file contains hidden or 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 withEntitySearch = <P,>(Component: ComponentType<P>) => (props: P) => { | |
const [target, setTarget] = useState<Range | null | undefined>(null); | |
const [index, setIndex] = useState(0); | |
const [searchState, setSearch] = useState<SearchState>(({ | |
searchTerm: '', | |
searchType: null, | |
} as unknown) as SearchState); | |
const optionsMenuRef = useRef<HTMLDivElement | null>(null); | |
// this is just a custom hook that calls an api |
This file contains hidden or 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
useEffect(() => { | |
let next = url; | |
let plans = []; | |
const fetch = async (url) => { | |
try { | |
setLoading(true); | |
const { data } = await axios(url); |
This file contains hidden or 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 { columns, data } = generateData(5); | |
const App = () => { | |
const [cols, setCols] = useState(columns); | |
const [rows, setRows] = useState(data); | |
const [dragOver, setDragOver] = useState(""); | |
const handleDragStart = e => { | |
const { id } = e.target; | |
const idx = cols.indexOf(id); |
This file contains hidden or 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 lang="en"> | |
<head> | |
<link | |
rel="stylesheet" | |
type="text/css" | |
href="paint-clone.css" | |
media="screen" | |
/> | |
<meta charset="UTF-8" /> |
NewerOlder