Button on card triggers setState, but does not re-render
See original GitHub issueHello everyone,
I have used this plugin for my project and really like it. Thank you for your work. I’m trying to implement couple transparent buttons on top of the card to go back and forth in an image array for each card (much like Instagram does it). The button does get called. However, the setState in the call does not make the UI to re-render. I have looked up what might cause it and used the solutions from other answers to trigger re-render and they work fine, but within the Card, it does not work for some reasons. Here are my codes:
return (
<View style={{ flex: 1 , backgroundColor: '#efede6', flexDirection: 'column'}}>
<View style={{ flex: 1 ,backgroundColor: 'blue'}}>
</View>
<View style={{ flex: 8 , backgroundColor: 'red'}}>
<Swiper
ref="deck"
cards={this.state.restaurants}
renderCard={(card) => {
return(
<ImageBackground
style = {styles.card}
source = {{uri:card.photos[this.state.currentImageIndex]}}
borderRadius = {20}
>
{this.renderImageIndexes()};
<View
style = {styles.imageControllerButtonContainer}
>
<TouchableOpacity
style = {styles.imageControllerButton}
onPressOut = {()=>this.previousImage()}/>
<TouchableOpacity
style = {styles.imageControllerButton}
onPressOut = {()=>this.nextImage()}
hitSlop={{ top: 12, left: 36, bottom: 0, right: 0 }}/>
</View>
</ImageBackground>
)
}}
onSwipedLeft={(cardIndex) => {
this.previousRestaurant(cardIndex);
}}
onSwipedRight={(cardIndex) => {
this.nextRestaurant(cardIndex);
}}
onSwipedAll={() => {console.log('onSwipedAll')}}
cardIndex={0}
backgroundColor={'#efede6'}
showSecondCard = {true}
stackSize= {this.state.restaurants.length}
disableLeftSwipe = {this.state.firstRestaurant}
goBackToPreviousCardOnSwipeLeft = {true}>
</Swiper>
</View>
</View>
);
And here is the call to setState:
nextImage(){
console.log("Get called!!!");
if(this.state.currentImageIndex < 10){
this.setState({
currentImageIndex: this.state.currentImageIndex + 1,
});
console.log(this.state.currentImageIndex)
}
}
previousImage(){
if(this.state.currentImageIndex > 0){
this.setState((state, props) => ({
currentImageIndex: this.state.currentImageIndex - 1,
}));
}
}
Appreciate any help
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
React: re render componet after button click - Stack Overflow
A simple way to re-render would be to change the state variable on the submission of the Axios request causing the component to...
Read more >React doesn't always trigger a re-render on setState
For new React developers, it is common to think that setState will trigger a re-render. But this is not always the case. If...
Read more >How to stop re-rendering lists in React? - Alex Sidorenko
Components always re-render. First, let's simplify our example by removing all props from the Item . We will still update the parent state...
Read more >setstate not rerendering | The AI Search Engine You Control
The problem lies in your shouldComponentUpdate . Right now, it only returns true when the props of the new clientID is different than...
Read more >Reconciliation - React
Just to be clear, rerender in this context means calling render for all components, it doesn't mean React will unmount and remount them....
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 Free
Top 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
@baophamtd that wouldn’t work and it looks really hard to maintain.
Really life saviour solution