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

173
Vistas
How do I make it so each user has its own state?

I have a like system similar to Instagram. The problem's that whenever I like a photo, the currentUserClicks state isn't its own state to all users

For example: There exists photos A and B. A has 10 likes B has 20 likes.

If I click like on photo A, then the like count correctly becomes 11

Right after, if I click like on photo B, then the like count incorrectly becomes 19 instead of 21

The currentUserClicks state thinks that photo B should be disliked (decrement the like count by 1 just like Instagram) because currentUserClicks becomes >= 1.

How can I make it so that each user has its own currentUserClicks value?

const [currentUserClicks, setCurrentUserClicks] = useState(null);

const handleLikesBasedOnUserId = (likedPhotoUserId) => {
    if(currentUserClicks >= 1) {
        setCurrentUserClicks(currentUserClicks - 1);
        handleDislike(likedPhotoUserId);
    } else {
        setCurrentUserClicks(currentUserClicks + 1);
        handleLike(likedPhotoUserId);
    }
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can "individualize" state in a couple basic ways.

  1. Push the state down to the components it pertains to.

    Here you move the currentUserClicks state into a React component that manages its own state and updates, completely independent of any other React component.

    Example:

    const InstagramPost = (props) => {
      const [liked, setLiked] = useState(false);
    
      const handleLikesBasedOnUserId = () => {
        if (liked) {
          handleDislike(id); // id from props
        } else {
          handleLike(id);
        }
        setCurrentUserClicks(liked => !liked);
      };
    
      ...
    
      return (
        ...
        <button onClick={handleLikesBasedOnUserId}>like</button>
        ...
      );
    };
    

    ...

    {posts.map(post => (
      <InstagramPost key={post.id} ....props.... />
    ))}
    
  2. Lift the state up to a common ancestor component and handle merging state updates.

    Example:

    const InstagramPost = ({ liked, likeHandler }) => {
      ...
      return (
        ...
        <button onClick={likeHandler}>like</button>
        ...
      );
    };
    

    ...

    const [liked, setLiked] = useState({});
    
    const handleLikesBasedOnUserId = (id) => {
      if (liked[id]) {
        handleDislike(id);
      } else {
        handleLike(id);
      }
      setCurrentUserClicks(liked => ({
        ...liked,
        [id]: !liked[id], // toggle liked state by id
      }));
    };
    
    ...
    
    {posts.map(post => (
      <InstagramPost
        key={post.id}
        liked={liked[post.id]}
        likeHandler={() => handleLikesBasedOnUserId(post.id)}
        ....props....
      />
    ))}
    
about 4 years ago · Juan Pablo Isaza Denunciar
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