<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Muskan Parashar</title>
    <description>The latest articles on DEV Community by Muskan Parashar (@muskanparashar).</description>
    <link>https://dev.to/muskanparashar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1048241%2F77e143ec-ad33-4f17-aa39-4512b42db499.jpg</url>
      <title>DEV Community: Muskan Parashar</title>
      <link>https://dev.to/muskanparashar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/muskanparashar"/>
    <language>en</language>
    <item>
      <title>React Native WebView: Make Android pullToRefresh work without any glitches</title>
      <dc:creator>Muskan Parashar</dc:creator>
      <pubDate>Tue, 11 Apr 2023 16:03:41 +0000</pubDate>
      <link>https://dev.to/muskanparashar/react-native-webview-make-android-pulltorefresh-work-without-any-glitches-467</link>
      <guid>https://dev.to/muskanparashar/react-native-webview-make-android-pulltorefresh-work-without-any-glitches-467</guid>
      <description>&lt;p&gt;With the help of below-mentioned pull request, you will find the pull-to-refresh feature for React Native web view Android platform. &lt;/p&gt;


&lt;div class="ltag_github-liquid-tag"&gt;
  &lt;h1&gt;
    &lt;a href="https://github.com/react-native-webview/react-native-webview/pull/2787" rel="noopener noreferrer"&gt;
      &lt;img class="github-logo" alt="GitHub logo" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg"&gt;
      &lt;span class="issue-title"&gt;
        Added support of pull down to refresh feature for Android platform.
      &lt;/span&gt;
      &lt;span class="issue-number"&gt;#2787&lt;/span&gt;
    &lt;/a&gt;
  &lt;/h1&gt;
  &lt;div class="github-thread"&gt;
    &lt;div class="timeline-comment-header"&gt;
      &lt;a href="https://github.com/muskan273" rel="noopener noreferrer"&gt;
        &lt;img class="github-liquid-tag-img" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Favatars.githubusercontent.com%2Fu%2F82081110%3Fv%3D4" alt="muskan273 avatar"&gt;
      &lt;/a&gt;
      &lt;div class="timeline-comment-header-text"&gt;
        &lt;strong&gt;
          &lt;a href="https://github.com/muskan273" rel="noopener noreferrer"&gt;muskan273&lt;/a&gt;
        &lt;/strong&gt; posted on &lt;a href="https://github.com/react-native-webview/react-native-webview/pull/2787" rel="noopener noreferrer"&gt;&lt;time&gt;Dec 26, 2022&lt;/time&gt;&lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag-github-body"&gt;
      
    &lt;/div&gt;
    &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/react-native-webview/react-native-webview/pull/2787" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Until the merge is done, you can integrate this feature into your app with the help of above pull request. &lt;/p&gt;

&lt;p&gt;While implementing the feature, I faced one issue i.e pull to refresh was not working well with the side modal and while refreshing the side modal page, pull to refresh triggers on scrolling up. In case you face this issue then the below solution will work for you. &lt;/p&gt;

&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;p&gt;We are required to make the changes from the website end and application end.&lt;br&gt;
Firstly, at the website end, we are required to take the callback on scrolling side modals by adding the below code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
//Added a custom hook to check the scroll value of all side modals

let canCheckScrollValue = true;
export function useSideModalScrollListener(props) {
    return useEffect(() =&amp;gt; {
            const allDiv = document.querySelectorAll(`${SIDE_MODAL_ID}`);
            allDiv?.forEach((item) =&amp;gt; {
                if (item &amp;amp;&amp;amp; item.getAttribute("listener") !== "true") {
                    item.setAttribute("listener", "true");
                    item.addEventListener("scroll", handleScroll);
                    return () =&amp;gt; item.removeEventListener("scroll", handleScroll);
                }
            });
    }, [props.open]);
}

const handleScroll = (event) =&amp;gt; {
    const scrollToTop = event.target?.scrollTop;
    if (scrollToTop === 0) {
        window?.ReactNativeWebView?.postMessage?.(JSON.stringify({ refreshEnabled: true }));
        canCheckScrollValue = true;
    } else if (canCheckScrollValue &amp;amp;&amp;amp; scrollToTop &amp;gt; 10) {
        window?.ReactNativeWebView?.postMessage?.(JSON.stringify({ refreshEnabled: false }));
        canCheckScrollValue = false;
    }
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;em&gt;props.open&lt;/em&gt; can set to true when any of the side modal will open&lt;br&gt;
&lt;em&gt;SIDE_MODAL_ID&lt;/em&gt; is a classname property of div element.&lt;/p&gt;

&lt;p&gt;Now we can use the above hook into the component where we want to check the value on scrolling side modal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
function SideModal() {
useSideModalScrollListener(props);
  return (
    &amp;lt;div className={`${SIDE_MODAL_ID}`}&amp;gt;
     ......
    &amp;lt;/div&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After adding a callback from the website end, we need to handle the same callback from the application end.&lt;/p&gt;

&lt;p&gt;Below is the code to handle callback at app end:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
function App() {

const onMessage = (event) =&amp;gt; { 
const { data } = event.nativeEvent;
if (data?.includes('{')) {
      const object = JSON.parse(data);
      if (object) {
        if (object?.refreshEnabled === true) {
        //trigger refresh when the scroll reaches to top
          webViewRef?.current?.enableAndroidRefresh();
        } else if (object?.refreshEnabled === false) {
        //disable refresh on scrolling down
          webViewRef?.current?.disableAndroidRefresh();
        }
      }
    }
}
  return (
      &amp;lt;WebView
        source={{
          uri: 'https://github.com/react-native-webview/react-native-webview',
        }}
        onMessage={onMessage}
      /&amp;gt;
    );
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webview</category>
      <category>reactnative</category>
      <category>javascript</category>
      <category>react</category>
    </item>
  </channel>
</rss>
