in Education by
This question already has answers here: useState set method not reflecting change immediately (17 answers) Closed 5 days ago. 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? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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]);

Related questions

0 votes
    This question already has answers here: useState set method not reflecting change immediately (17 answers) Closed ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Hi I have to pass array as props to a functional component. Show code snippet But it is converted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Hi I have to pass array as props to a functional component. Show code snippet But it is converted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    Hi I have to pass array as props to a functional component. Show code snippet But it is converted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
0 votes
    I'm a bit surprised I'm having trouble finding this online, but I can't seem to find an example ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I'm a bit surprised I'm having trouble finding this online, but I can't seem to find an example ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I'm a bit surprised I'm having trouble finding this online, but I can't seem to find an example ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I am trying to calculate the progress of a file upload, so I was using Superagent. I was able to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    This is my object. values : { title : 'this is title ..', translate : { en : 'some texts ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    This is my object. values : { title : 'this is title ..', translate : { en : 'some texts ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    I have have no error in my code. But my condition, finally don't work. const togglePeronsHandler = ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have have no error in my code. But my condition, finally don't work. const togglePeronsHandler = ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    I have have no error in my code. But my condition, finally don't work. const togglePeronsHandler = ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    Currently working on my portfolio. I used this template. I'm trying to make my profile image into a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
...