Last active
December 2, 2019 12:54
-
-
Save iremlopsum/48702e85b09a210662ccd31c7d306896 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class ScrollView extends PureComponent { | |
state = { | |
// This actually lives within redux for me, but this list will be updated as the user navigates through chatbot | |
chatItems: [ | |
{ | |
message: 'Hello, this is bot', | |
}, | |
{ | |
message: 'I am here to help you', | |
}, | |
], | |
} | |
renderChatItems = (item, index, chatList) => { | |
return <ChatItem chatEntry={item} key={index} isLastItem={(chatList.length - 1) === index} /> | |
} | |
render() { | |
return ( | |
<ScrollView>{this.state.chatItems.map(this.renderChatItems)}</ScrollView> | |
) | |
} | |
} | |
class ChatItem extends PureComponent { | |
avatarOpacityAnimatedValue = new Animated.Value(Number(this.props.isLastItem)) | |
componentWillReceiveProps(nextProps) { | |
if (this.props.isLastItem !== nextProps.isLastItem) { | |
// I want to animate the component hidden without rerendering | |
Animated.timing(this.avatarOpacityAnimatedValue, { | |
toValue: Number(nextProps.isLastItem), | |
duration: 300, | |
}).start() | |
} | |
} | |
shouldComponentUpdate = () => false | |
render() { | |
return ( | |
<View> | |
<Animated.View style={{ opacity: this.avatarOpacityAnimatedValue }}> | |
<Avatar /> | |
</Animated.View> | |
</View> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment