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

271
Visualizações
Child not rendering after parent state updates

I am making a memory type game as my first project in react native, but im running into some trouble with rerendering the children when the parent updates.

The child component:

import React from "react";
import { Pressable } from "react-native";
export default class GameCard extends React.Component
{
    render()
    {
        return(
            <Pressable
                style={[{backgroundColor: this.props.isOpen ? this.props.color : 'coral'}, styles.card]}
                onPress={this.props.handleClick}
            />
        )
    }
}


const styles = {
    card: {
        width: 77, height: 135,
        margin: 10,
        marginVertical: 30,
    }
};

The parent:

import React, {Component, useState} from "react";
import {generateCards} from '../data/cards.js'
import Gamecard from "../components/GameCard.js";
import {FlatList, View, Text} from "react-native";

export default class Game extends Component {
    state = {
        score: 0,
        currentSelection: [],
        selectedPairs: [],
        cards: []
    }


    componentDidMount() {
        this.setState({
            cards:generateCards(),
        })
    }

    handleClick = id => {
        let selectedPairs = [...this.state.selectedPairs];
        let currentSelection = this.state.currentSelection;
        let score = this.state.score;
        let localCards = this.state.cards;

        if(currentSelection.length < 2 && !currentSelection.includes(localCards[id]))
        {
            currentSelection.push(localCards[id]);
        }

        localCards[id].isOpen = true;

        this.setState({
            cards: localCards,
            currentSelection: currentSelection
        })

    }

    renderGameCard = ({item}) => {
        return (
            <Gamecard
                color={item.color}
                key={item.key}
                isOpen={item.isOpen}
                handleClick={this.handleClick.bind(this, item.key)}
            />
        )
    }

    renderFlatList ()
    {
        let cardData = this.state.cards;
        return (
            <View style={{justifyContent: 'center', alignItems: 'center'}}>
                <FlatList
                    data={cardData}
                    renderItem={this.renderGameCard}
                    numColumns={4}
                    extraData={this.state.currentSelection}
                />
            </View>
        )
    }

    render()
    {
       return this.renderFlatList();
    }
}

Basically, when one of the children is clicked, i want to change its background color and perform some logic.

only problem is that the child component does not rerender when clicked, even though its isOpen property is updated in the parent state, can anyone explain / help me resolve this problem, Thanks in advance.

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