react-nativereact-native-render-html

Typewriter effect not working RenderHtml component (React - Native)


I have chat based app where i am getting response in html formate i want to render it with typewriter effect. I am using below packages for typewriter and render html

'react-native-typewriter' for typewriter and react-native-render-html for rending html content.

Typewriter effect is not working please find code snippet below.

import RenderHtml from 'react-native-render-html';
import TypeWriter from 'react-native-typewriter';


export default function App() {
    return (
        <TypeWriter typing={1}
        onTypingEnd={() => console.log('logg')}
        minDelay={1}
        maxDelay={1}
        >
            <RenderHtml
              source={{ html:'<p>hi i am rendering html</p>'}}
            />
        </TypeWriter>
}

I have tried multiple approach to render it but didn't work out. what i want is to render html content with typewriting effect in react native. it would be great if someone can help me out.


Solution

  • As TypeWriter is a wrapper for Text component so it can't work with HTML so i found another approach so we can give typewriter effect to html content.

    I am calling printSentenceSet(result?.data?.id, resText).

    const printSentenceSet = (id, sentence, speed = 50) => {
        let index = 0;
      
        let timer = setInterval(function() {
          const char = sentence[index];
          
          if (char === '<') {
            index = sentence.indexOf('>', index);  
          }
          setCompResObj(
            {
              [id]: sentence.slice(0, index)
            }
          )
          if (++index === sentence.length) {
            clearInterval(timer);
          }
        }, speed);
      }
    

    compResObj is an useState variable

    <RenderHTML 
       contentWidth={width}
       tagsStyles={mixedStyle} 
       source={{html:compResObj[props.currentMessage._id]}} 
    />