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

149
Visualizações
React Component doesn't rerender when the state is changed

I am working on a small React project to get to learn more about it.

The project can make and view groups and in the groups you can store images, videos...

So the problem is when I open one of those groups and while I'm in it if I try to change to another group the component doesn't rerender and I have the same information.

Here's some of the code:

App:

function App() {
  const [chosenGroup, setChosenGroup] = useState({});

return (
    <Router>
      <Header setChosenGroup={setChosenGroup}/>
      <Routes>
        <Route 
          path="/Group/:group_name" 
          element={<Group group={chosenGroup} />}
        />
      </Routes>
    </Router>
  );

}

Header (Where I choose the group):

function Header(props) {
  const setChosenGroup = props.setChosenGroup;

  return (
    <>
      <Top>
        <Image src={SiteLogo} />
        <GroupsBtn>
          MY GROUPS
        </GroupsBtn>
        <AllGroupsCont>
          <AllGroups>
            {myGroupDB.map((group, index) => {
              return (
                <StyledLink to={`/Group/${group.group_name}`} key={index}>
                  <Group onClick={() => {
                      setChosenGroup(group);
                    }}>
                    {group.group_name}
                  </Group>
                </StyledLink>
              );
            })}
          </AllGroups>
        </AllGroupsCont>
      </Top>
    </>
  )
}

Group:

function Group(props) {
  const param = useParams();
  
  return (
    <Main>
      <Top>
        <GroupTitle defaultValue={param.group_name} />
        <SaveButton className="saveButtonGroup" src={SaveIcon} />
        <DateCreated>Date created: {creationDate}</DateCreated>
      </Top>
      <Center>
        <GridContainer>
          {group.photos.map((photo) => {
            <Image src={photo.url} />;
          })}
        </GridContainer>
      </Center>
      <Bottom>
        <GoBackBtn onClick={() => navigate(-1)}>GO BACK</GoBackBtn>
        <SaveBtn>SAVE</SaveBtn>
      </Bottom>
    </Main>
  )
}

I checked the components in the browser and the state does indeed update so the problem is something else.

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

0

This might solve it

  <Group onClick={() => {
                      setChosenGroup({...group});
 }}>

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