13

Now I would like to pull down a scrollView and refresh this component, so I have followed the official document mentioning onRefresh and RefreshContorol from react-native.

However, I don't know why my code is not working and get error...

The code below is my code.

   <View style={styles.container}>
    <ScrollView
      contentContainerStyle={{ flexDirection: 'row', flexWrap: 'wrap' }}
      refreshControl={<RefreshControl refreshing={this.state.refreshing}  onRefresh={this.setState({ refreshing: true })} />}
    >
      {this.renderItemBox()}
    </ScrollView>
  </View>
2
  • can you add errors? Commented May 8, 2019 at 4:17
  • Please add error which you are facing during execute this code. Commented May 8, 2019 at 4:27

3 Answers 3

24

Below is the sample code in which you can find RefreshController integration with ScrollView:

import { ScrollView, RefreshControl } from 'react-native';
    
class RefreshableList extends Component {
  constructor(props) {
    super(props);
    this.state = {
      refreshing: false,
    };
  }

  _onRefresh = () => {
    this.setState({refreshing: true});
    fetchData().then(() => {
      this.setState({refreshing: false});
    });
  }

  render() {
    return (
      <ScrollView
        refreshControl={
          <RefreshControl
            refreshing={this.state.refreshing}
            onRefresh={this._onRefresh}
          />
        }
      />
    );
  }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Every time pull request, new Data keep duplicated. @Nirmalsinh
6

on me it worked

import React from 'react';
import {
  RefreshControl,
  ScrollView,
  StyleSheet,
  View,
} from 'react-native';
import {WebView} from 'react-native-webview';

const wait = timeout => {
  return new Promise(resolve => setTimeout(resolve, timeout));
};

const Main = () => {
  const webViewRef = React.useRef();
  const [refreshing, setRefreshing] = React.useState(false);
  const [enablePTR, setEnablePTR] = React.useState(false);

  
  const onRefresh = React.useCallback(() => {
    webViewRef.current.reload();
    setRefreshing(true);
    wait(2000).then(() => setRefreshing(false));
  }, []);

  const handleEvent = e => {
    if (e.nativeEvent.contentOffset.y > 100) {
      setEnablePTR(false);
    } else {
      setEnablePTR(true);
    }
  };
  return (
    <View style={styles.container}>
      <ScrollView
        scrollEnabled={false}
        contentContainerStyle={styles.container}
        refreshControl={
          <RefreshControl
            refreshing={refreshing}
            onRefresh={onRefresh}
            enabled={enablePTR}
          />
        }
      >
        <WebView
          source={{ uri: "https://example.com/" }}
          ref={(ref) => (webViewRef.current = ref)}
          javaScriptEnabled={true}
          injectedJavaScript="setTimeout(() => {document.addEventListener('scroll', function (event) {window.ReactNativeWebView.postMessage(JSON.stringify(document.getElementsByClassName('topconteiner')[0].scrollTop));},true);}, 300);true;"
          onScroll={(event) => handleEvent(event)}
        />
      </ScrollView>
    </View>
  );
};

export default Main;

const styles = StyleSheet.create({
  container: {backgroundColor: '#fff', flex: 1},
});

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
1
import {ScrollView,RefreshControl,SafeAreaView} from 'react-native';
import React,{useState,useEffect} from 'react';

export default function App({ navigation }) {
const [refreshing,setRefreshing] =useState(false);

useEffect(()=>{
   pullToRefreshFunction() 
},[]}

const pullToRefreshFunction = () => {
     setRefreshing(true)
     ..... your api calling here..
     setRefreshing(false)
}

 return(
 <SafeAreaView style={{flex:1}}>
 <ScrollView  refreshControl={
   <RefreshControl
  refreshing={refreshing}
  onRefresh={pullToRefreshFunction }
/>}
>
</ScrollView>
</SafeAreaView>
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.