Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!