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

188
Views
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
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!