Add memoization
This commit is contained in:
parent
f459f4c20c
commit
e99e5c35ea
|
@ -1,4 +1,4 @@
|
|||
import React from "react";
|
||||
import React, { useMemo } from "react";
|
||||
import { useSelectionContext } from "../useSelection";
|
||||
|
||||
type AddressHighlighterProps = React.PropsWithChildren<{
|
||||
|
@ -10,12 +10,15 @@ const AddressHighlighter: React.FC<AddressHighlighterProps> = ({
|
|||
children,
|
||||
}) => {
|
||||
const [selection, setSelection] = useSelectionContext();
|
||||
const select = () => {
|
||||
const [select, deselect] = useMemo(() => {
|
||||
const _select = () => {
|
||||
setSelection({ type: "address", content: address });
|
||||
};
|
||||
const deselect = () => {
|
||||
const _deselect = () => {
|
||||
setSelection(null);
|
||||
};
|
||||
return [_select, _deselect];
|
||||
}, [setSelection, address]);
|
||||
|
||||
return (
|
||||
<div
|
||||
|
|
|
@ -1,23 +1,29 @@
|
|||
import React, { useState, useContext } from "react";
|
||||
import {
|
||||
Dispatch,
|
||||
SetStateAction,
|
||||
createContext,
|
||||
useState,
|
||||
useContext,
|
||||
} from "react";
|
||||
|
||||
export type Selection = {
|
||||
type: "address" | "value";
|
||||
content: string;
|
||||
};
|
||||
|
||||
type OptionalSelection = Selection | null;
|
||||
|
||||
export const useSelection = (): [
|
||||
Selection | null,
|
||||
React.Dispatch<React.SetStateAction<Selection | null>>
|
||||
OptionalSelection,
|
||||
Dispatch<SetStateAction<OptionalSelection>>
|
||||
] => {
|
||||
const [selection, setSelection] = useState<Selection | null>(null);
|
||||
return [selection, setSelection];
|
||||
return useState<OptionalSelection>(null);
|
||||
};
|
||||
|
||||
export const SelectionContext = React.createContext<
|
||||
ReturnType<typeof useSelection>
|
||||
>(null!);
|
||||
export const SelectionContext = createContext<ReturnType<typeof useSelection>>(
|
||||
null!
|
||||
);
|
||||
|
||||
export const useSelectionContext = () => {
|
||||
const ctx = useContext(SelectionContext);
|
||||
return ctx;
|
||||
return useContext(SelectionContext);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue