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

117
Views
Set local storage in a react Js method

I have a simple voting system (Yes or No) if you click Yes the state needs to be a string up and if you click No the state needs to be down, i need to set local storage in order to prevent the user from keeping voting, do i have to do this in useEffect or just in the voteHandler method, is there an alternative of the local storage ?

Here is my code :

import React, { useState, useContext } from 'react';
import {AnswerContext} from './AnswerWrapper';

const AnswerItem = (props) => {

    let indexPlus;

    const indexCount = (index) => {
        indexPlus = index;
        return indexPlus;
    }

    const { active, setActive } = useContext(AnswerContext)

    const [vote, setVote] = useState();

    const voteHandler = (e, index, value) => {
        e.preventDefault();
        localStorage.setItem('myVoteInLocalStorage', value);
        setActive(index);
        setVote(value)
        // I am sending this to a parent component
        props.onVote ({
            vote : value,
            answerID : index
        })
    }

    return (
        <div>
            <button onClick={(e) => voteHandler(e, props.index, 'up')}>Yes <span>({props.ups !== null ? props.ups : 0 })</span></button>
            <button onClick={(e) => voteHandler(e, props.index, 'down')}>No <span>({props.downs !== null ? props.downs : 0 })</span></button>
        </div>
    )
}

export default AnswerItem;
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!