-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RFC: Memoize style objects to prevent re-renders of memoized components
- Loading branch information
1 parent
bfb6b30
commit d127771
Showing
5 changed files
with
63 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<View style={styles.container}> | ||
<Memoized text="With breakpoints" style={styles.withBreakpoints} /> | ||
<Memoized text="No breakpoints" style={styles.noBreakpoints} /> | ||
<Memoized text="Static" style={staticStyles.static} /> | ||
<Button title={`Re-render (${count})`} onPress={() => setCount(count => count + 1)} /> | ||
</View> | ||
) | ||
} | ||
|
||
const Memoized = React.memo<{text: string; style: StyleProp<TextStyle>}>(({ text, style }) => | ||
<Text style={style}>{text} ({useRenderCount()})</Text> | ||
) | ||
|
||
const useRenderCount = () => { | ||
const count = React.useRef(0) | ||
count.current++ | ||
|
||
return count.current | ||
} | ||
|
||
const stylesheet = createStyleSheet({ | ||
container: { | ||
flex: 1, | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
alignContent: 'space-between', | ||
resizeMode: 'contain' | ||
}, | ||
withBreakpoints: { | ||
fontSize: 18, | ||
color: { | ||
xs: 'red', | ||
md: 'blue' | ||
} | ||
}, | ||
noBreakpoints: { | ||
fontSize: 18 | ||
} | ||
}) | ||
|
||
const staticStyles = createStyleSheet({ | ||
static: { | ||
fontSize: 18 | ||
} | ||
}) |
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 |
---|---|---|
@@ -1,11 +1,4 @@ | ||
import { useWindowDimensions } from 'react-native' | ||
import type { ScreenSize } from '../types' | ||
|
||
export const useDimensions = (): ScreenSize => { | ||
const { width, height } = useWindowDimensions() | ||
|
||
return { | ||
width, | ||
height | ||
} | ||
} | ||
export const useDimensions = (): ScreenSize => useWindowDimensions() |