2

I'm trying to re-render the child component when pull down refresh from parent but no idea how to do so. Here is what I currently have in my code.

From parent :

OnRefresh = () => {
    this.setState({refresh: true});
    // call function loadTaskListing() from child component
    this.setState({refresh: false});
}

render() {
    return (
        <View>
            <ScrollView refreshControl={<RefreshControl refreshing={this.state.refresh} onRefresh={this.OnRefresh} />}>
                <TaskListing navigation={this.props.navigation} />
            </ScrollView>
        </View>
    )
}

From child (TaskListing) :

loadTaskListing(userId){
    // get data from server side
}

render() {
    return (
        <View>
            { /* listing view goes here */ }
        </View>
    )
}

Based on my code above, refresh control is from parent view but the function I want to reload is from child view. How am I going to trigger the function from child and return to parent to get refreshed?

Jaydeep Galani
  • 4,842
  • 3
  • 27
  • 47
Emerald
  • 864
  • 1
  • 14
  • 37

1 Answers1

2

You can use ref to call child's method in parent.

OnRefresh = () => {
    this.setState({refresh: true});
    // call method of child component using ref
     this.taskListRef.loadTaskListing();
}

refreshDone =()=>{
  this.setState({refresh: false});
}

render() {
    return (
        <View>
            <ScrollView refreshControl={<RefreshControl refreshing={this.state.refresh} onRefresh={this.OnRefresh} />}>
                <TaskListing 
                   ref={instance=>this.taskListRef = instance} // ref assigning 
                   navigation={this.props.navigation}
                   refreshDone={this.refreshDone}
                />
            </ScrollView>
        </View>
    )
}

call this.props.refreshDone() in child when you want to stop refreshing.

Jaydeep Galani
  • 4,842
  • 3
  • 27
  • 47