You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1003 B

2 years ago
import { useEffect, useRef } from 'react';
export function createElementObject(instance, context, container) {
return Object.freeze({
instance,
context,
container
});
}
export function createElementHook(createElement, updateElement) {
if (updateElement == null) {
return function useImmutableLeafletElement(props, context) {
return useRef(createElement(props, context));
};
}
return function useMutableLeafletElement(props, context) {
const elementRef = useRef(createElement(props, context));
const propsRef = useRef(props);
const { instance } = elementRef.current;
useEffect(function updateElementProps() {
if (propsRef.current !== props) {
updateElement(instance, props, propsRef.current);
propsRef.current = props;
}
}, [
instance,
props,
context
]);
return elementRef;
};
}