Skip to content

Commit

Permalink
Fixing useRef() usage in createElementHook to prevent unnecessary Lea…
Browse files Browse the repository at this point in the history
…flet object creation (#1014)
  • Loading branch information
abac committed Aug 14, 2022
1 parent 5e45d08 commit fd9fde6
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions packages/core/src/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@ export function createElementHook<E, P, C = any>(
props: P,
context: LeafletContextInterface,
): ReturnType<ElementHook<E, P>> {
return useRef<LeafletElement<E, C>>(createElement(props, context))
const elementRef = useRef<LeafletElement<E, C>>() as MutableRefObject<
LeafletElement<E>
>
if (!elementRef.current)
elementRef.current = createElement(props, context)
return elementRef
}
}

return function useMutableLeafletElement(
props: P,
context: LeafletContextInterface,
): ReturnType<ElementHook<E, P>> {
const elementRef = useRef<LeafletElement<E, C>>(
createElement(props, context),
)
const elementRef = useRef<LeafletElement<E, C>>() as MutableRefObject<
LeafletElement<E>
>
if (!elementRef.current) elementRef.current = createElement(props, context)
const propsRef = useRef<P>(props)
const { instance } = elementRef.current

Expand Down

0 comments on commit fd9fde6

Please sign in to comment.