From d127771b158cfce8c1e825c4b434f147aeca05a3 Mon Sep 17 00:00:00 2001 From: Levi Buzolic Date: Wed, 11 Oct 2023 23:52:21 +1100 Subject: [PATCH] RFC: Memoize style objects to prevent re-renders of memoized components --- example/src/App.tsx | 7 +--- example/src/examples/Memoization.tsx | 54 ++++++++++++++++++++++++++++ example/src/examples/index.ts | 1 + src/createUnistyles.ts | 11 +++--- src/hooks/useDimensions.ts | 9 +---- 5 files changed, 63 insertions(+), 19 deletions(-) create mode 100644 example/src/examples/Memoization.tsx diff --git a/example/src/App.tsx b/example/src/App.tsx index e0f56d14..a763aee4 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -16,12 +16,7 @@ export const App: React.FunctionComponent = () => { return ( - setTheme(prevState => prevState === Theme.Light - ? Theme.Dark - : Theme.Light - )} - /> + ) } diff --git a/example/src/examples/Memoization.tsx b/example/src/examples/Memoization.tsx new file mode 100644 index 00000000..5778da33 --- /dev/null +++ b/example/src/examples/Memoization.tsx @@ -0,0 +1,54 @@ +import React from 'react' +import { Text, View, type StyleProp, type TextStyle, Button } from 'react-native' +import { createStyleSheet, useStyles } from '../styles' + +export const Memoization: React.FunctionComponent = () => { + const { styles } = useStyles(stylesheet) + const [ count, setCount ] = React.useState(0) + + return ( + + + + +