Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

313
Views
Animate text with React (typing effect)

I am trying to animate text with React. Like a typewriter. I am using setTimeout and setState. Here is my code. Is there a way to make it faster and with smoother animation? Thanks

const [text, setText] = useState<string>(' ')
  const animateText = (direction: boolean) => {
    description.substring(120, description.length)
    const tick = description.length - 120
    console.log('direction', direction)

    if (direction) {
      for (let index = 0; index < tick; index++) {
        setTimeout(() => {
          setText(description.substring(120, index))
        }, index * 0.01)
      }
    } else {
      for (let index = 0; index < tick; index++) {
        setTimeout(() => {
          setText(description.substring(120, description.length - index))
        }, index * 0.01)
      }
    }
  }

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your code is missing some attributes like the event that triggers the animatedText. But triggering it on first component load (see the useEffect) and adding a time rate (index * 50) based on the previous timeout, you can do this:

const {
  useState,
  useCallback,
  useEffect
} = React;

const description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

const Component = () => {
  const [text, setText] = useState(' ')

  const animateText = useCallback((direction) => {
    const max = description.length;

    for (let index = 0; index < max; index++) {
      const startIndex = direction ? 0 : max
      const endIndex = direction ? index : max - index
      setTimeout(() => {
        setText(description.substring(startIndex, endIndex))
      }, index * 50)
    }
  }, [])

  useEffect(() => {
    animateText(true)
  }, [animateText])

  return text
}

// Render it
ReactDOM.render( < Component / > ,
  document.getElementById("react")
);
#react {
  width: 300px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!