Pages are not rendered when first render pass involves empty data set
See original GitHub issueBug report
Summary
Current behavior
When the data used to generate children
for <ViewPager />
is initially empty ([]
), subsequent render passes with actual non-empty lists of data do create the correct children/pages: No ViewPager
pages content is rendered at all.
Expected behaviour
ViewPager
should correctly render its children
. Previous (e.g. empty) children
states should not affect the rendering behaviour.
Environment info
react-native info
output:
System:
OS: macOS 10.15.5
CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
Memory: 500.04 MB / 16.00 GB
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 14.5.0 - ~/.nvm/versions/node/v14.5.0/bin/node
Yarn: 1.17.3 - ~/.yarn/bin/yarn
npm: 6.14.5 - ~/.nvm/versions/node/v14.5.0/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
Managers:
CocoaPods: 1.9.3 - /usr/local/bin/pod
SDKs:
iOS SDK:
Platforms: iOS 13.5, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
Android SDK:
API Levels: 23, 27, 28
Build Tools: 27.0.3, 28.0.1, 28.0.3
Android NDK: Not Found
IDEs:
Android Studio: 3.1 AI-173.4819257
Xcode: 11.5/11E608c - /usr/bin/xcodebuild
Languages:
Java: 1.8.0_201 - /usr/bin/javac
Python: 3.7.6 - /opt/anaconda3/bin/python
npmPackages:
@react-native-community/cli: Not Found
react: ~16.11.0 => 16.11.0
react-native: https://github.com/expo/react-native/archive/sdk-38.0.2.tar.gz => 0.62.2
npmGlobalPackages:
*react-native*: Not Found
Library version: 3.3.0, 4.1.6
This behavior has been reproduced on iPhone X and the iOS simulator (both running iOS 13.5.1).
Steps to reproduce
- Add
<ViewPager />
to your view hierarchy - Add custom child/page elements generated (e.g. via
Array.map()
) from dynamic data - Define data/children to be initially empty
- Initialize data to display in
ViewPager
, e.g. fetch data from remote web service
=> Non-empty data should be rendered correctly, but is not.
Reproducible sample code
import * as React from 'react'
import { useEffect, useState } from 'react'
import { Text, View } from 'react-native'
import ViewPager from '@react-native-community/viewpager'
export const BugReproductionContext = () => {
// Observation: Initializing with a non-empty array will prevent the bug
const [items, setItems] = useState<string[]>([])
useEffect(() => {
setTimeout(() => {
setItems(['one', 'two', 'three'])
}, 1000)
}, [setItems])
// Workaround: prevent empty ViewPager initialization
// if (!items.length) {
// return null
// }
return (
<ViewPager
style={{ width: '100%', height: 50, backgroundColor: '#ddd' }}
initialPage={0}
>
{/*<View key="-1">*/}
{/* <Text>A static item which is always rendered. Adding it will also prevent the bug. </Text>*/}
{/*</View>*/}
{items.map((item, index) => (
<View key={index}>
<Text>Item: {item}</Text>
</View>
))}
</ViewPager>
)
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Why is useState not triggering re-render? - Stack Overflow
You're calling setNumbers and passing it the array it already has. You've changed one of its values but it's still the same array,...
Read more >Just Say No to Excessive Re-Rendering in React - GrapeCity
In this article, we will address instances of excessive re-rendering in React applications and demonstrate how to avoid them.
Read more >When does React re-render components? - Felix Gerschau
In the first part of this article, I'll explain the most important concepts about rendering in React and how React decides to re-render...
Read more >How To Handle Async Data Loading, Lazy Loading, and Code ...
Notice that the component renders before the data is loaded. The advantage with asynchronous code is that it won't block the initial render....
Read more >React re-renders guide: everything, all at once - Developer way
There is no way to prevent a component that uses a portion of Context value from re-rendering, even if the used piece of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Actually, I have the same issue on ios first array is empty then after API call we set the state but noting show on the screen
same issue on iOS here, I solved it by making the key of the PagerView dependent on whether the dataset is empty.