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

190
Views
Get data using useeffect hook in react js

I have a simple page editor, When a user clicks edit page it opens an editor. I am passing the ID of the page using redux which will be used to get data from API.

Here is my Editor.

const [pageData, setPageData] = useState("");

  const getPage = async (id) => {
    try {
      const response = await api.get(`/landing_pages/${id}`);
      console.log("page", response.data); // displays data at the end
      setPageData(response.data);
    } catch (error) {
      console.log(error);
    }
  };

  useEffect(() => {

    getPage(pageID);

    console.log('Page Data', pageData) // displays nothing

    let LandingPage = pageData;

     const editor = grapesjs.init({
       container: "#editor",
       components: LandingPage.components || LandingPage.html,
     })

  }, [pageID, getPage])

Why is Page Data display nothing even though the data from API is returned and is displayed in the console at the end? what am I doing wrong here?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Even if you await your getPage call, the updated pageData won't be available until the next render cycle so your assignment to LandingPage will be one cycle behind.

You should instead update in one useEffect and watch for changes to pageData in another.

const [pageData, setPageData] = useState("");

  useEffect(() => {
    const getPage = async (id) => {
      try {
        const response = await api.get(`/landing_pages/${id}`);
        console.log("page", response.data); // displays data at the end
        setPageData(response.data);
      } catch (error) {
        console.log(error);
      }
    };

    getPage(pageID);
  }, [pageID]);

  useEffect(() => {
    console.log('Page Data', pageData); // displays updated pageData

    let LandingPage = pageData;

    const editor = grapesjs.init({
      container: "#editor",
      components: LandingPage.components || LandingPage.html,
    });
  }, [pageData]);
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!