(
createElement: (
props: P,
context: LeafletContextInterface,
) => LeafletElement<E>,
updateElement?: (instance: E, props: P, prevProps: P) => void,
)
| 22 | ) => RefObject<LeafletElement<E>> |
| 23 | |
| 24 | export function createElementHook<E, P, C = unknown>( |
| 25 | createElement: ( |
| 26 | props: P, |
| 27 | context: LeafletContextInterface, |
| 28 | ) => LeafletElement<E>, |
| 29 | updateElement?: (instance: E, props: P, prevProps: P) => void, |
| 30 | ) { |
| 31 | if (updateElement == null) { |
| 32 | return function useImmutableLeafletElement( |
| 33 | props: P, |
| 34 | context: LeafletContextInterface, |
| 35 | ): ReturnType<ElementHook<E, P>> { |
| 36 | const elementRef = useRef<LeafletElement<E, C>>(undefined) as RefObject< |
| 37 | LeafletElement<E> |
| 38 | > |
| 39 | if (!elementRef.current) |
| 40 | elementRef.current = createElement(props, context) |
| 41 | return elementRef |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return function useMutableLeafletElement( |
| 46 | props: P, |
| 47 | context: LeafletContextInterface, |
| 48 | ): ReturnType<ElementHook<E, P>> { |
| 49 | const elementRef = useRef<LeafletElement<E, C>>(undefined) as RefObject< |
| 50 | LeafletElement<E> |
| 51 | > |
| 52 | if (!elementRef.current) elementRef.current = createElement(props, context) |
| 53 | const propsRef = useRef<P>(props) |
| 54 | const { instance } = elementRef.current |
| 55 | |
| 56 | useEffect( |
| 57 | function updateElementProps() { |
| 58 | if (propsRef.current !== props) { |
| 59 | updateElement(instance, props, propsRef.current) |
| 60 | propsRef.current = props |
| 61 | } |
| 62 | }, |
| 63 | [instance, props, updateElement], |
| 64 | ) |
| 65 | |
| 66 | return elementRef |
| 67 | } |
| 68 | } |
no test coverage detected
searching dependent graphs…