Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
Save Draft button to avoid loosing the data on a page refresh React

I try to store it in local storage but it did not work for me. It give me a error when I click of save draft button.

import React, { useEffect, useState } from "react";
import "./App.css";

function App() {
  const [text, setText] = useState("");
  const [totalcharacters, setTotalCharacters] = useState(0);
  const [totalword, setTotalWord] = useState(0);

  const handleDraft = (text) => {
    if (localStorage.setItem("text", JSON.stringify(text))) {
      setText(localStorage.getItem("text"));
    }
  };

  useEffect(() => {
    setTotalCharacters(text.length);

    localStorage.setItem("text", text);

    function findTotal(text) {
      text = text.replace(/(^\s*)|(\s*$)/gi, "");
      text = text.replace(/[ ]{2,}/gi, " ");
      text = text.replace(/\n /, "\n");
      setTotalWord(text.split(" ").length);
    }
    findTotal(text);
    handleDraft(text);
    // setTotalWord(text.split(' ').length)
  }, [text, totalword]);
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda