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

154
Visualizações
React Parent Function Called from Child Component does not Update State?

I'm stuck on having my child component: ChildButton call its parent component: ParentTable's function that contains a "setState", to then refresh or update the table itself (in the parent).

I am passing a getData function from the parent to the child through props. On button click in the child - call a function to; close a dialog, do a PUT on a mock api datastore, then call the getData function. The getData function does a GET from the api and does a setState on "apiData". Problem is the data is not updating in the parent table even though the getData function is being called.. Don't state changes cause a reload? Code below:

ParentTable.jsx

export default function ParentTable() {
    **const [apiData, setAPIData] = useState([]);**

    const getData = () => {
        axios.get(`https://blah.mockapi.io/fakeData`)
            .then((getData) => {
                **setAPIData**(getData.data);
            })
        console.log('getData'); // fires on child button click
        }

    return (
        <Table..
            <Table.Body>
                {**apiData**.map((obj) => {
                    <Table.Cell>bunch of data from the obj..
                    <Table.Cell><ChildButton **getData={getData}**</Table.Cell>
                    
        ... />../>../>
    )
}

export default ParentTable;

ChildButton.jsx

const ChildButton = ({getData, ...}) => {
    function handleClose() {
        setOpen(false)
        axios.put(`https://blah.mockapi.io/fakeData/${id}`, {
            ...
        })
        **getData()**

    }

    return (
        <Button onClick={handleClose} ...>
            Update
        </Button>
    )
}

export default ChildButton;

Long story short - why is my change of state in the Parent Component not updating it's table data?

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

0

Axios.put returns a promise and you are not waiting for the response. Invoke the getData function inside the .then function or use async await:

 axios.put(`https://blah.mockapi.io/fakeData/${id}`, {
            ...
        }).then(() => **getData()**)
        
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