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

101
Visualizações
useState is working but i dont see the changes

im coding a chess game to learn react. So, i got this on the Board component:

export default () => {
    const[squares,setSquares] = useState([]);
    
    useEffect(() => { 
        let item;
        let squaresAux = [];
        setBoard(FEN).forEach((square,i) => {
            item = {
                value: square,
                pos: getXYPosition(i),
                color: getColor(i)        
            }
            squaresAux.push(item);
        })
        setSquares(squaresAux);
    },[])

   

    const handleClick = () => {
        let update = squares.map(square => {
            square.color = "active";
            return square;
        })

        setSquares(update);

    }

    return(
        <div className="board">
            {squares.map((square) => (  
                <Square handleClick = {handleClick} color={square.color} value={square.value} />
            ))}
        </div>
    );
}

And on the Square component i got this:

import React,{useState} from 'react';

    

export default (props) => {
    const[value,setValue] = useState(props.value);
    const[color,setColor] = useState(props.color);

    const handleClick = () => {
        props.handleClick();
        
    }

    return(
        <div>
            <div className={`cell ${color}`} onClick={() => handleClick()}>{value}</div>
        </div>
    );
}

The idea is that when i click on a square all the squares change their color. The state is changing in both components, also the color state in square is changing, but the visual colors dont. ¿What could be the problem?

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