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

327
Visualizações
Maintaining checkbox value on local Storage React

I am trying to save the value of checkboxes in local storage, so when the user reloads the page, they remain checked/unchecked. I have an "isChecked" state and a handleOnChange function.

What im trying to do is store the value on the state, and every time the onChange fuction runs, i set the 'checkbox' key on local storage to the value of the checkbox.

this is what my component looks like

import React from "react";
import { useState, useEffect } from "react";
const Pdf = (props) => {
  const [shown, setShown] = useState(false);

  const [isChecked, setIsChecked] = useState(localStorage.getItem('checkbox') === 'true');

  const handleOnChange = (e) => {
      setIsChecked(localStorage.getItem('checkbox'));
      localStorage.setItem('checkbox',`${e.target.checked}`)
  }
  

  const toggle = () => {
    setShown((prevState) => !prevState);
    alert(isChecked)
  };

  
  return (
    <div className="pdfContainer  row container justify-content-center">
      <section className="justify-content-center">
        <h1 className="row justify-content-center h1--style">
          Κεφάλαιο {props.id}
        </h1>
        <p className="row justify-content-center p--style">{props.info}</p>
      </section>
      <button onClick={toggle} className=" shadow button--style">
        Μάθημα {props.id}
      </button>
      {shown && <embed className=" pdf" src={props.pdf} />}

      <div className="pdfChecked input-group-text input-group ">
        <input 
            type="checkbox"
            id='pdfchecked'
            name="pdfChecked" 
            value='1'
            checked={isChecked}
            onChange={handleOnChange}
            /> <label className="input--label"> Διάβασα το κεφάλαιο!</label>
      </div>
    </div>
  );
};

export default Pdf;

I understand that this is very buggy, and very much not how you're supposed to do it. I found the local storage concept hard to grasp as a beginner, and i have tried many ways to make this work. As of now, it kinda works, meaning when i reload the page it stays the same, but i have to click on the checkbox multiple times! SO buggy. Any help, especially letting me know why my thought process is wrong, would be appreciated!

about 4 years ago · Juan Pablo Isaza
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