Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc-tools/doc-pages/src/pages/PageFAQ.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default function PageFAQ() {
</Section>
</Chapter>
<Chapter title="Troubleshooting">
<Section title="Warning: You seem to update the X prop of the Y component in short periods of time...">
<Section title="Warning: You seem to pass a new X value with identical content to the Y component on every render...">
<Paragraph>
There is a detailed explaination for this warning here:{' '}
<Hyperlink url="https://stackoverflow.com/q/68966120/2779871">
Expand Down
5 changes: 3 additions & 2 deletions packages/render/src/RenderHTMLConfigProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ export default function RenderHTMLConfigProvider(
...sharedProps
} = props;
const engine = useAmbientTRenderEngine();
const profile = useProfiler({ prop: 'remoteErrorView or remoteLoadingView' });
const profile = useProfiler({});
const sourceLoaderConfig = useMemo(() => {
typeof __DEV__ === 'boolean' && __DEV__ && profile();
typeof __DEV__ === 'boolean' && __DEV__ &&
profile({ remoteErrorView, remoteLoadingView });
return {
remoteErrorView: remoteErrorView || defaultRenderError,
remoteLoadingView: remoteLoadingView || defaultRenderLoading
Expand Down
7 changes: 3 additions & 4 deletions packages/render/src/RenderHTMLSource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,10 @@ const RenderHTMLSource = memo(
contentWidth,
...props
}: RenderHTMLSourceProps) {
const profile = useProfiler({
prop: 'onDocumentMetadataLoaded or onTTreeChange'
});
const profile = useProfiler({});
const ttreeEvents: TTreeEvents = useMemo(() => {
typeof __DEV__ === 'boolean' && __DEV__ && profile();
typeof __DEV__ === 'boolean' && __DEV__ &&
profile({ onDocumentMetadataLoaded, onTTreeChange });
return {
onDocumentMetadataLoaded,
onTTreeChange
Expand Down
4 changes: 2 additions & 2 deletions packages/render/src/context/RenderRegistryProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export default function RenderRegistryProvider({
elementModels: HTMLElementModelRecord;
renderers?: CustomTagRendererRecord;
}>) {
const profile = useProfiler({ prop: 'renderers' });
const profile = useProfiler({});
const registry = useMemo(() => {
typeof __DEV__ === 'boolean' && __DEV__ && profile();
typeof __DEV__ === 'boolean' && __DEV__ && profile({ renderers });
return new RenderRegistry(renderers, elementModels);
}, [renderers, elementModels, profile]);
return (
Expand Down
5 changes: 3 additions & 2 deletions packages/render/src/context/RenderersPropsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ export function useRendererProps<
export default function RenderersPropsProvider(
props: PropsWithChildren<RenderHTMLPassedProps>
) {
const profile = useProfiler({ prop: 'renderersProps' });
const profile = useProfiler({});
const mergedRenderersProps = useMemo(() => {
typeof __DEV__ === 'boolean' && __DEV__ && profile();
typeof __DEV__ === 'boolean' && __DEV__ &&
profile({ renderersProps: props.renderersProps });
return mergeDeepRight(defaultRendererProps, props.renderersProps || {});
}, [props.renderersProps, profile]);
return React.createElement(
Expand Down
166 changes: 166 additions & 0 deletions packages/render/src/hooks/__tests__/useProfiler.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import React, { StrictMode } from 'react';
import { render, renderHook } from '@testing-library/react-native';
import RenderHTML from '../../RenderHTML';
import { RenderHTMLProps } from '../../shared-types';
import useProfiler from '../useProfiler';

function getRerenderWarnings() {
return (console.warn as jest.Mock).mock.calls
.map(([m]) => m)
.filter((m) => typeof m === 'string' && m.startsWith('You seem to pass'));
}

describe('useProfiler', () => {
beforeEach(() => {
console.warn = jest.fn();
});

function setup() {
return renderHook(() => useProfiler({ name: 'Comp' })).result.current;
}

it('should not warn when the same references are profiled (StrictMode replays)', () => {
const profile = setup();
const renderers = {};
for (let i = 0; i < 5; i++) {
profile({ renderers });
}
expect(console.warn).not.toHaveBeenCalled();
});

it('should not warn when a value is recreated only once', () => {
const profile = setup();
profile({ tagsStyles: { p: { color: 'red' } } });
profile({ tagsStyles: { p: { color: 'red' } } });
expect(console.warn).not.toHaveBeenCalled();
});

it('should not warn when content genuinely changes', () => {
const profile = setup();
for (let i = 0; i < 5; i++) {
profile({ baseStyle: { opacity: i / 5 } });
}
expect(console.warn).not.toHaveBeenCalled();
});

it('should warn once per prop, naming only recreated props', () => {
const profile = setup();
const stable = () => {};
for (let i = 0; i < 6; i++) {
profile({
renderersProps: { a: { onPress() {} } },
onTTreeChange: stable
});
}
expect(console.warn).toHaveBeenCalledTimes(1);
const message = (console.warn as jest.Mock).mock.calls[0][0] as string;
expect(message).toContain(
'a new "renderersProps" value with identical content to the "Comp" component'
);
expect(message).toContain('useMemo or useCallback');
expect(message).not.toContain('onTTreeChange');
});

it('should compare nested objects and arrays by content', () => {
const profile = setup();
for (let i = 0; i < 3; i++) {
profile({ tagsStyles: { p: { color: 'red' } }, list: [1, { a: 2 }] });
}
expect(console.warn).toHaveBeenCalledTimes(1);
const message = (console.warn as jest.Mock).mock.calls[0][0] as string;
expect(message).toContain('new "tagsStyles" and "list" values');
});

it('should treat functions with different source as genuine changes', () => {
const profile = setup();
profile({ onPress: () => 1 });
profile({ onPress: () => 2 });
profile({ onPress: () => 3 });
expect(console.warn).not.toHaveBeenCalled();
});

it('should not inspect class instances', () => {
const profile = setup();
for (let i = 0; i < 3; i++) {
profile({ date: new Date(0) });
}
expect(console.warn).not.toHaveBeenCalled();
});

it('should terminate on cyclic structures', () => {
const profile = setup();
const makeCyclic = () => {
const o: any = {};
o.self = o;
return o;
};
expect(() => {
for (let i = 0; i < 3; i++) {
profile({ cyclic: makeCyclic() });
}
}).not.toThrow();
});

it('should list several recreated props in one warning', () => {
const profile = setup();
for (let i = 0; i < 3; i++) {
profile({ a: {}, b: [], c: () => null });
}
expect(console.warn).toHaveBeenCalledTimes(1);
const message = (console.warn as jest.Mock).mock.calls[0][0] as string;
expect(message).toContain('new "a", "b" and "c" values');
});
});

describe('RenderHTML re-render warnings', () => {
const source = { html: '<p>Hello</p>' };
beforeEach(() => {
console.warn = jest.fn();
});

function renderTimes(
getProps: (i: number) => Partial<RenderHTMLProps>,
times: number
) {
const { rerender } = render(
<StrictMode>
<RenderHTML contentWidth={100} source={source} {...getProps(0)} />
</StrictMode>
);
for (let i = 1; i < times; i++) {
rerender(
<StrictMode>
<RenderHTML contentWidth={100} source={source} {...getProps(i)} />
</StrictMode>
);
}
}

it('should not warn on mount in StrictMode, even with inline props', () => {
renderTimes(
() => ({ renderers: {}, renderersProps: {}, tagsStyles: {} }),
1
);
expect(getRerenderWarnings()).toEqual([]);
});

it('should warn about inline props on repeated re-renders', () => {
renderTimes(() => ({ tagsStyles: { p: { color: 'red' } } }), 4);
const warnings = getRerenderWarnings();
expect(warnings).toHaveLength(1);
expect(warnings[0]).toContain('"tagsStyles"');
expect(warnings[0]).toContain('"TRenderEngineProvider"');
});

it('should not warn when props are stable across re-renders', () => {
const tagsStyles = { p: { color: 'red' } };
const renderersProps = { a: { onPress() {} } };
renderTimes(() => ({ tagsStyles, renderersProps }), 4);
expect(getRerenderWarnings()).toEqual([]);
});

it('should not warn when props genuinely change on re-renders', () => {
renderTimes((i) => ({ baseStyle: { fontSize: 10 + i } }), 4);
expect(getRerenderWarnings()).toEqual([]);
});
});
Loading