Add a Clear All Complete Button to the Footer with React Native TouchableOpacity

Jason Brown
InstructorJason Brown

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 3 years ago

In this lesson we'll add a button to the footer that will clear all completed items. We'll use TouchableOpacity and we'll examine how flex space-around property works.

[00:00] We'll start by rendering a TouchableOpacity. Inside of there, we'll create a text element and say ClearCompleted. Now if we go over and we refresh, you can see that we have a ClearCompleted button rendering and there's space around all of the items. Now we'll go add an onPress to our TouchableOpacity and say this.props.onClearCompleted.

[00:23] Now, we'll switch back to our app and we'll add a handle ClearCompleted to be a function. Go ahead and bind this, this.handleClearCompleted = this.handleClearCompleted.bind(this). Now, we'll type const newItems = filterItems active, so we'll filter our items for only the active items, which will remove our completed items. We'll then say this.setSource and give it our new items, so we'll replace our array of items.

[01:04] We'll also filter items, this.state.filter and apply the filter to the new, the only active items. Now, we'll pass this into our footer. We'll say onClearCompleted = this.handleClearCompleted. When we go and we refresh our emulator, we can add an item, add another, you can toggle one of these to complete and click ClearCompleted and see that it disappeared.