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

114
Visualizações
Calculating state happens to late

I want to make a simple like button, which has the appropriate color, depending if it is liked or not. I try calculate this in advance (before the return) with React.useEffect, but it seems to calculate it afterwards... I can see it because all the hearts, are not colored, but when I do a soft reload, they change to the right color... Anybody knows what I'm doing wrong here?

const Hart = ({subject}) => {
    const [liked, setLiked] = useState(false);
    const [hasloaded,setHasloaded] = useState(false);
    const [ownId, setOwnId] = useState('');
    const [token, setToken] = useState('');
    const [favourite, setFavourite] = useState([]);
    const favouriteId = [];
    React.useEffect(()=> {
        const constructor = async () => {
            await refreshToken();
          
          
              //favourites contains all the favorite subjects
              //This calculates the boolean to check if the subject is liked or not
              //This array is calculated with axios, and is correct
            for(let i = 0; i<favourite.length; i++) {
                if(favourite[i].id === subject.id) setLiked(true)
            }
            setHasloaded(true);
            console.log("done")
        }
        constructor();
    },[])

    if(!hasloaded) return null;
    else{
        return (
            <Pressable style= {styleSubjectList.heartIcon} onPress={() => checkFavorite()}>
                <MaterialCommunityIcons
                    name={liked ? "heart" : "heart-outline"}
                    size={20}
                    color={liked ? "red" : "white"}
                />
            </Pressable>
        );
    }
  };
  export default Hart;

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The function constructor is async and you are setting two states which are also async functions. Thus, the for loop which eventually triggers setLiked will not be finished before sethasLoaded is set to true. You need to synchronize both operations.

const constructor = async () => {
   await refreshToken();
         
   for(let i = 0; i<favourite.length; i++) {
       if(favourite[i].id === subject.id) setLiked(true)
   }
}
constructor().then(() => setHasloaded(true)).catch(e => console.log(e))
about 4 years ago · Juan Pablo Isaza Relatório

0

React.useEffect(()=> {
        const constructor = async () => {
            await refreshToken();

            let token = await getAccessToken();
            setToken(token);
            let id = await getFromStore("ownId");
            id = removeFirstAndLast(id)
            setOwnId(id)

            const axios = require('axios' );
            const config = {
                method: 'get',
                url: backendURL + '/userManagement/users/' + id,
                headers: {
                    'Authorization': 'Bearer ' + JSON.parse(token)
                }
            };

            axios(config)
                .then(function (response) {
                    setFavourite(response.data.favouriteSubjects)
                    // console.log(response.data.favouriteSubjects)
                })
                .catch(function (error) {
                    console.log(error);
                });

            for(let i = 0; i<favourite.length; i++) {
                if(favourite[i].id === subject.id) setLiked(true)
            }
        }
        constructor().then(() => {
            setHasloaded(true);
            console.log("done");
        }).catch(e=>console.log(e));
    },[])

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