-
-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: limit rerenderings via memoization
- Loading branch information
Showing
15 changed files
with
162 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react'; | ||
import useTTree from './hooks/useTTree'; | ||
import { RenderResolvedHTMLProps } from './shared-types'; | ||
import TDocumentRenderer from './TDocumentRenderer'; | ||
|
||
export default function RenderResolvedHTML(props: RenderResolvedHTMLProps) { | ||
const ttree = useTTree(props); | ||
return ( | ||
<TDocumentRenderer | ||
tdoc={ttree} | ||
baseUrl={props.baseUrl} | ||
onDocumentMetadataLoaded={props.onDocumentMetadataLoaded} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import React, { PropsWithChildren, useCallback, useMemo } from 'react'; | ||
import { TextProps, ViewProps } from 'react-native'; | ||
import selectSharedProps from '../helpers/selectSharedProps'; | ||
import { | ||
RenderHTMLFragmentProps, | ||
RenderHTMLSharedProps, | ||
TRendererBaseProps | ||
} from '../shared-types'; | ||
import defaultSharedProps from './defaultSharedProps'; | ||
|
||
const SharedPropsContext = React.createContext<Required<RenderHTMLSharedProps>>( | ||
defaultSharedProps | ||
); | ||
|
||
export function useSharedProps< | ||
RendererProps extends Record<string, any> = Record<string, any> | ||
>() { | ||
return React.useContext(SharedPropsContext) as Required< | ||
RenderHTMLSharedProps<RendererProps> | ||
>; | ||
} | ||
|
||
export function useRendererProps< | ||
RendererProps extends Record<string, any> = Record<string, any>, | ||
K extends keyof RendererProps = any | ||
>(k: K) { | ||
return useSharedProps<RendererProps>().renderersProps[k]; | ||
} | ||
|
||
export function useDefaultContainerProps(): Pick< | ||
TRendererBaseProps<any>, | ||
'viewProps' | 'textProps' | ||
> { | ||
const sharedProps = useSharedProps(); | ||
return { | ||
viewProps: { | ||
...defaultSharedProps.defaultViewProps, | ||
...sharedProps.defaultViewProps | ||
}, | ||
textProps: { | ||
...defaultSharedProps.defaultTextProps, | ||
...sharedProps.defaultTextProps | ||
} | ||
}; | ||
} | ||
export function useDefaultTextProps(): TextProps { | ||
return { | ||
...defaultSharedProps.defaultTextProps, | ||
...useSharedProps().defaultTextProps | ||
}; | ||
} | ||
|
||
export function useDefaultViewProps(): ViewProps { | ||
return { | ||
...defaultSharedProps.defaultViewProps, | ||
...useSharedProps().defaultViewProps | ||
}; | ||
} | ||
|
||
export function useComputeMaxWidthForTag(tagName: string) { | ||
const { computeEmbeddedMaxWidth } = useSharedProps(); | ||
return useCallback( | ||
(cw: number) => { | ||
return computeEmbeddedMaxWidth(cw, tagName); | ||
}, | ||
[computeEmbeddedMaxWidth, tagName] | ||
); | ||
} | ||
|
||
export default function SharedPropsProvider( | ||
props: PropsWithChildren<RenderHTMLFragmentProps> | ||
) { | ||
const memoizedSharedProps = useMemo( | ||
() => selectSharedProps(props), | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
Object.values(selectSharedProps(props)) | ||
); | ||
return React.createElement( | ||
SharedPropsContext.Provider, | ||
{ value: memoizedSharedProps }, | ||
props.children | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Dimensions, Linking } from 'react-native'; | ||
import { RenderHTMLSharedProps } from '../shared-types'; | ||
|
||
const defaultSharedProps: Required<RenderHTMLSharedProps> = { | ||
debug: false, | ||
contentWidth: Dimensions.get('window').width, | ||
enableExperimentalPercentWidth: false, | ||
defaultTextProps: { | ||
selectable: false, | ||
allowFontScaling: true | ||
}, | ||
defaultViewProps: {}, | ||
enableExperimentalMarginCollapsing: false, | ||
computeEmbeddedMaxWidth: (contentWidth) => contentWidth, | ||
imagesInitialDimensions: { | ||
height: 50, | ||
width: 50 | ||
}, | ||
onLinkPress: (_e, href) => Linking.canOpenURL(href) && Linking.openURL(href), | ||
WebView: () => { | ||
if (__DEV__) { | ||
console.warn( | ||
'One of your renderer is attempting to use WebView component, which has not been ' + | ||
"provided as a prop to the RenderHtml component. As a consequence, the element won't be rendered." | ||
); | ||
} | ||
return null; | ||
}, | ||
defaultWebViewProps: {}, | ||
renderersProps: {}, | ||
setMarkersForTNode: () => null | ||
}; | ||
|
||
export default defaultSharedProps; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.