question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RenderItem doesn't refresh when state change

See original GitHub issue

Is this a bug report, a feature request, or a question?

A bug report

Have you followed the required steps before opening a bug report?

Have you made sure that it wasn’t a React Native bug?

it’s not a react native bug.

Is the bug specific to iOS or Android? Or can it be reproduced on both platforms?

I just test to Android.

Is the bug reproductible in a production environment (not a debug one)?

I don’t use it to prod. I check in my phone with react-native run-android.

Environment

Environment: React: 16.8.6 React native: 0.60.5 react-native-snap-carousel: 3.8.2

Target Platform: Android (6.0)

Expected Behavior

I have 3 items. Each item is a button that opens the camera. Once we take the picture, it should show the picture instead of the button for each item.

Actual Behavior

When I take the photo, it updates the state but it does not display the photo.

Reproducible Demo

<Carousel
                        ref={(c) => { this._carousel = c; }}
                        data={[
                            {key: 0, urlImage: this.state.urlImage1,},
                            {key: 1, urlImage: this.state.urlImage2,},
                            {key: 2, urlImage: this.state.urlImage3,},
                        ]}
                        extraData={this.state}
                        renderItem={this._renderItem}
                        sliderWidth={screen.width}
                        sliderHeight={300}
                        itemWidth={300}
                        itemHeight={300}
                        containerCustomStyle={{
                            flexGrow: 0,
                        }}
                    />
_renderItem ({item, index}) {
       return (
           <View style={{ width: 300, height: 300, position: "relative", marginTop: 20 }}> 
               { item.urlImage == "data:image/jpeg;base64" ? 
                   <TouchableOpacity 
                       onPress={(e) => this.takePhoto(e, item.urlImage)}  
                       style={{ backgroundColor: 'transparent', borderColor: 'white', borderWidth: 1 ,width: 300, height: 300,  zIndex: 9999, elevation: 9999, position: "absolute", top: 0, left: 0, right: 0, bottom: 0, justifyContent: 'center', alignItems: 'center' }}
                   >
                       <View>
                           <Icon 
                               type="MaterialIcons"
                               name="add-a-photo"
                               size={95}
                               color={"white"}
                           />
                       </View>
                   </TouchableOpacity>
                   : <View></View> 
               }
               <Image
                   source={{ uri : item.urlImage }}
                   style={{ width: 300, height: 300, backgroundColor: "#046381" }}
               />
           </View>
       );
   }
takePhoto(e, item) {
        ImagePicker.launchCamera(options, (response) => {
            if (response.error) {
                console.log('ImagePicker Error: ', response.error);
            } 
            else {
                const source = { uri: 'data:image/jpeg;base64,' + response.data };
                this.setState({
                    [item]: source.uri,
                    refresh: !this.state.refresh
                }, () => { console.log(this.state.refresh) });
            }
        })
    }

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:9
  • Comments:10

github_iconTop GitHub Comments

4reactions
kasim444commented, Dec 2, 2019

Hi. I don’t know if you’ve solved the problem, but I had the same problem today. If you do as in the example below, the high probability of the problem will be solved. I hope it helps.

state = { refresh: false, };

<Carousel ref={© => { this._carousel = c; }} data={[ {key: 0, urlImage: this.state.urlImage1,}, {key: 1, urlImage: this.state.urlImage2,}, {key: 2, urlImage: this.state.urlImage3,}, ]} extraData={this.state.refresh} renderItem={this._renderItem} sliderWidth={screen.width} sliderHeight={300} itemWidth={300} itemHeight={300} containerCustomStyle={{ flexGrow: 0, }} />

takePhoto(e, item) { ImagePicker.launchCamera(options, (response) => { if (response.error) { console.log('ImagePicker Error: ', response.error); } else { const source = { uri: ‘data:image/jpeg;base64,’ + response.data }; this.setState({ [item]: source.uri, refresh: !this.state.refresh }, () => { console.log(this.state.refresh) }); this.setState({ refresh: !refresh}); } }) }

0reactions
DenisBevilacquacommented, Feb 9, 2022

You can try use triggerRenderingHack().

useEffect( () => {
	carouselRef.current.triggerRenderingHack();
}, [ items ] );

...

<Carousel
	ref={carouselRef}
	data={items}
	renderItem={renderItem}
	onSnapToItem={_setCarouselIndex}
	sliderWidth={screenWidth}
	itemWidth={screenWidth}
/>

Read more comments on GitHub >

github_iconTop Results From Across the Web

RenderItem doesn't refresh when state change #610 - GitHub
I have an issue with updating the state in component. In my case, I am changing the width of the sliderWidth and itemWidth...
Read more >
react native Flatlist renderItem not update - Stack Overflow
But when I'm updating product quantity, it only increases total amount but nothing changes in renderItem of FlatList. enter image description ...
Read more >
Usage | FlashList
Check your renderItem hierarchy for components that make use of useState and verify whether that state would need to be reset if a...
Read more >
FlatList - React Native
Pull to Refresh. ... we make sure FlatList itself will re-render when the state changes. ... otherwise your UI may not update on...
Read more >
react-native flatlist does not re-render on element update?
react native render view does not update after state changes auth component · React Native : FlatList does not render · React native...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found