Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

253
Visualizações
How to wrap text typed with 'typed.js' ReactJs?

I'm building a web app with React that generates random movie quotes... Here is a preview of the web app

The problem arises when the quote is too long and it overflows outside the parent div... I've tried altering the css with display flex and flex-wrap set to wrap. It does't work. Here is my code.

import React from 'react';
import Typed from 'typed.js';
import './App.css';
import quotes from './quotes.json';

const random_quote = () => {
  const rand = Math.floor(Math.random() * quotes.length);
  let selected_quote = quotes[rand].quote + ' - ' + quotes[rand].movie;
  return selected_quote;
}

const TypedQuote = () => {
    // Create reference to store the DOM element containing the animation
    const el = React.useRef(null);
  // Create reference to store the Typed instance itself
    const typed = React.useRef(null);

  React.useEffect(() => {
    const options = {
        strings: [
        random_quote(),
      ],
      typeSpeed: 30,
      backSpeed: 50,
    };
    
    // elRef refers to the <span> rendered below
    typed.current = new Typed(el.current, options);
    
    return () => {
      // Make sure to destroy Typed instance during cleanup
      // to prevent memory leaks
      typed.current.destroy();
    }
  }, [])

  return (
      <div className="type-wrap">
        <span style={{ whiteSpace: 'pre' }} ref={el} />
      </div>
  );
}

const App = () => {
  return (
    <>
      <div className='background' id='background'>
        <div className='quote-box'>
          <TypedQuote />
        </div>

        <button onClick={random_quote}>New Quote</button>
      </div>
    </>
  );
}

export default App;

I have this idea where I could implement a function that adds '\n' after like 10 words or like maybe after a '.' or ',' (I could implement some logic here). But this seems like a longshot. Is there a fancier way to do this?? Any help would be appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try the property below on the parent container.

word-wrap: break-word;

or the below if you want to break words as well

word-break: break-all;
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda