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

107
Views
React setState don't update values inside a promise

I'm trying to store data with my setState method inside my function but it never update it, however if I set them to a variable (I call it "x") it works.

I have my function which returns a Promise

I hope I can get help figuring out where I went wrong or understand if it cannot be done or not.

import { NextPage } from "next";
import { useState } from "react";
import { useAirtable } from "../functions/useAirtable";
import { MyType } from "../model/MyType";

const Foo: NextPage = () => {

  const [barbaz, setBarbaz] = useState<any>([]);
  let x: any = [];

  try {
    useAirtable("My_Table").then((data: MyType) => {

        setBarbaz(data);
        x = data;

        console.log("print1: ", data); //It PRINTS data
        console.log("print2: ", barbaz); //It PRINTS empty array
        console.log("print3: ", x); //It PRINTS data
      }
    );
  } catch (error) {}

  console.log("print4: ", barbaz); //It PRINTS empty array
  console.log("print5: ", x); //It PRINTS empty array

  return <div> where is waldo </div>;
};

export default Foo;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Wrap your async call on a useEffect hook.

import { NextPage } from "next";
import { useState, useEffect } from "react";
import { useAirtable } from "../functions/useAirtable";
import { MyType } from "../model/MyType";

const Foo: NextPage = () => {

  const [barbaz, setBarbaz] = useState<any>([]);
  
  useEffect(() => {
    useAirtable("My_Table").then((data: MyType) => {
      setBarbaz(data);
    }
  }, [])

  return <div> where is waldo </div>;
};

export default Foo;
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!