Skip to content

Commit

Permalink
fix(rerender): allow for multiple Rerender components to update in tree
Browse files Browse the repository at this point in the history
release-npm
  • Loading branch information
tobua committed Jan 16, 2024
1 parent e056792 commit 35d8bf4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
10 changes: 3 additions & 7 deletions index.ts
Expand Up @@ -80,7 +80,9 @@ const app = {
rerender: () => {
updateBreakpoint()
app.listener.forEach((listener) => listener())
app.rerenderComponents.forEach((update) => update())
},
rerenderComponents: [] as Function[],
// Calculates a scaled value.
value: linearScale,
// Updates the current breakpoint depending on window width.
Expand Down Expand Up @@ -219,13 +221,7 @@ export const Rerender = ({
style?: StyleProp<ViewStyle> | StyleProp<ViewStyle>[]
}) => {
const [count, setCount] = useState(0)

app.rerender = () => {
updateBreakpoint()
app.listener.forEach((listener) => listener())
setCount(count + 1)
}

app.rerenderComponents.push(() => setCount(count + 1))
return createElement(View, { style, key: count, children: children() })
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -43,14 +43,14 @@
"flexbox"
],
"devDependencies": {
"@react-native-community/cli": "^12.3.2",
"@react-native-community/cli": "^12.3.3",
"@react-native/babel-preset": "^0.74.0",
"@react-native/eslint-config": "^0.74.0",
"@react-native/typescript-config": "^0.74.0",
"@testing-library/jest-native": "^5.4.3",
"@testing-library/react-native": "^12.4.3",
"@types/jest": "^29.5.11",
"@types/node": "^20.11.2",
"@types/node": "^20.11.4",
"@types/react": "^18.2.48",
"@types/react-native": "^0.72.8",
"@types/react-test-renderer": "^18.0.7",
Expand Down
23 changes: 23 additions & 0 deletions test/basic.test.tsx
Expand Up @@ -632,3 +632,26 @@ test('MobX observer components will also be rerendered.', () => {
expect(regularRenders).toBe(2)
expect(observedRenders).toBe(2)
})

test('Multiple Rerender components can be placed in the React tree.', () => {
let renders = 0
const RegularComponent = () => {
renders += 1
return <View accessibilityLabel="regular" style={styles.wrapper} />
}
render(
<>
<Rerender>{() => <RegularComponent />}</Rerender>
<Rerender>{() => <RegularComponent />}</Rerender>
<Rerender>{() => <RegularComponent />}</Rerender>
</>,
)

expect(renders).toBe(3)

act(() => {
rerender()
})

expect(renders).toBe(6)
})

0 comments on commit 35d8bf4

Please sign in to comment.