Typewriter library | How to add < & > in typeString()?

41 Views Asked by At

I want to escape < and > characters within the typeString() method in Typewriter.

I've tried using the escape characters, but it does not seem to work.

This is the code I'm writing.

<Typewriter
      onInit={(typewriter) => {
        typewriter
          .typeString("<div>import Aadesh from 'org'</div>")
          .pauseFor(1500)
          .typeString("<div>const App() =&#62; &#60; Aadesh /&#62; </div>")
          .start();
      }}
    />

It does not convert &#60; to < and &#62; to >. I've also tried using &lt; & &gt; but it hasn't worked.

Expected output from typewriter:

import Aadesh from 'Org' 
const App() => <Aadesh />

Actual output form typewriter

import Aadesh from 'Org' 

const App() => `&lt;` Aadesh /`&gt;`
1

There are 1 best solutions below

1
Keyboard Corporation On BEST ANSWER

When you pass a string with html tags to the typeString(), the library treats the entire string as a single entity and does not parse or render the tags. This is why &lt; and &gt; are not being converted to < and >. If that so, use js built-in string methods to replace the entities with the actual characters before passing the string to the typeString().

Example;

<Typewriter
 onInit={(typewriter) => {
   typewriter
     .typeString("<div>import Aadesh from 'org'</div>")
     .pauseFor(1500)
     .typeString("<div>const App() =&#62; &#60; Aadesh /&#62; </div>".replace(/&#60;/g, '<').replace(/&#62;/g, '>'))
     .start();
 }}
/>