Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, { useRef } from 'react';
- import { WebView } from 'react-native-webview';
- import axios from 'axios';
- const MyWebComponent = () => {
- const webViewRef = useRef(null);
- const handleMessage = (event) => {
- const { data } = event.nativeEvent;
- const parsedData = JSON.parse(data);
- if (parsedData.type === 'scrollPosition') {
- saveScrollPosition(parsedData.position);
- }
- };
- const saveScrollPosition = async (position) => {
- try {
- await axios.post('https://your-api.com/save-position', { position });
- } catch (error) {
- console.error('Error saving position:', error);
- }
- };
- const injectedJavaScript = `
- document.addEventListener('scroll', () => {
- const scrollY = window.scrollY || document.documentElement.scrollTop;
- window.ReactNativeWebView.postMessage(JSON.stringify({ type: 'scrollPosition', position: scrollY }));
- });
- true;
- `;
- return (
- <WebView
- ref={webViewRef}
- source={{ uri: 'https://your-web-content.com' }}
- onMessage={handleMessage}
- injectedJavaScript={injectedJavaScript}
- />
- );
- };
- export default MyWebComponent;
Advertisement
Comments
-
- add a backend service to store the user's reading position in a db. Save the current loc when the user navigates away. When opening the ebk on a different dvc, fetch this saved loc and set it as the starting point, enabling seamless transitions across
Add Comment
Please, Sign In to add comment
Advertisement