MCPcopy Create free account
hub / github.com/ampproject/amphtml / RenderWithRef

Function RenderWithRef

extensions/amp-render/1.0/component.js:27–107  ·  view source on GitHub ↗
(
  {
    ariaLiveValue = 'polite',
    getJson = DEFAULT_GET_JSON,
    onError,
    onLoad,
    onLoading,
    onRefresh,
    render = DEFAULT_RENDER,
    src = '',
    ...rest
  },
  ref
)

Source from the content-addressed store, hash-verified

25 * @return {PreactDef.Renderable}
26 */
27export function RenderWithRef(
28 {
29 ariaLiveValue = 'polite',
30 getJson = DEFAULT_GET_JSON,
31 onError,
32 onLoad,
33 onLoading,
34 onRefresh,
35 render = DEFAULT_RENDER,
36 src = '',
37 ...rest
38 },
39 ref
40) {
41 useResourcesNotify();
42
43 const [data, setData] = useState({});
44 const onLoadRef = useValueRef(onLoad);
45 const onErrorRef = useValueRef(onError);
46
47 useEffect(() => {
48 // TODO(dmanek): Add additional validation for src
49 // when adding url replacement logic.
50 if (!src) {
51 return;
52 }
53 let cancelled = false;
54 onLoading?.();
55 getJson(src)
56 .then((data) => {
57 if (!cancelled) {
58 setData(data);
59 }
60 })
61 .catch((e) => {
62 onErrorRef.current?.(e);
63 });
64 return () => {
65 cancelled = true;
66 };
67 }, [getJson, src, onErrorRef, onLoading]);
68
69 const refresh = useCallback(() => {
70 onRefresh?.();
71 getJson(src, /* shouldRefresh */ true)
72 .then((data) => {
73 setData(data);
74 onLoadRef.current?.();
75 })
76 .catch((e) => {
77 onErrorRef.current?.(e);
78 });
79 }, [getJson, src, onLoadRef, onRefresh, onErrorRef]);
80
81 useImperativeHandle(
82 ref,
83 () =>
84 /** @type {!RenderDef.RenderApi} */ ({

Callers

nothing calls this directly

Calls 7

useResourcesNotifyFunction · 0.90
useValueRefFunction · 0.90
useRendererFunction · 0.90
setDataFunction · 0.85
catchMethod · 0.80
currentMethod · 0.80
thenMethod · 0.45

Tested by

no test coverage detected