{"id":6028,"date":"2023-02-27T12:23:11","date_gmt":"2023-02-27T12:23:11","guid":{"rendered":"https:\/\/www.goodacademic.com\/blog\/questions\/computer-systems-question\/"},"modified":"2023-02-27T12:23:11","modified_gmt":"2023-02-27T12:23:11","slug":"computer-systems-question","status":"publish","type":"questions","link":"https:\/\/www.goodacademic.com\/blog\/questions\/computer-systems-question\/","title":{"rendered":"Computer Systems Question"},"content":{"rendered":"<div class=\"col-sm-12 messageContent\">\n <b>Learning Goal: <\/b>I&#8217;m working on a computer systems and need to help me learn.<\/p>\n<p>here&#8217;s an example React code that uses <code>fetch()<\/code> combined with <code>useEffect()<\/code> to load data from a REST API and display it on the page::-<\/p>\n<p>import React, { useState, useEffect } from &#8220;react&#8221;;<\/p>\n<p>function App() {<\/p>\n<p>const [data, setData] = useState([]);<\/p>\n<p>useEffect(() =&gt; {<\/p>\n<p>async function fetchData() {<\/p>\n<p>const response = await fetch(&#8220;https:\/\/example.com\/api\/data&#8221;);<\/p>\n<p>const json = await response.json();<\/p>\n<p>setData(json);<\/p>\n<p>}<\/p>\n<p>fetchData();<\/p>\n<p>}, []);<\/p>\n<p>return (<\/p>\n<p>&lt;div&gt;<\/p>\n<p>{data.map((item) =&gt; (<\/p>\n<p>&lt;div key={item.id}&gt;{item.title}&lt;\/div&gt;<\/p>\n<p>))}<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>);<\/p>\n<p>}<\/p>\n<p>export default App;<\/p>\n<p>In this example, we&#8217;re using <code>useState()<\/code> to create a state variable called <code>data<\/code>, which we&#8217;ll use to store the data that we fetch from the API. We&#8217;re also using <code>useEffect()<\/code> with an empty dependency array to fetch the data when the component mounts for the first time.<\/p>\n<p>Inside the <code>useEffect()<\/code> hook, we define an async function called <code>fetchData()<\/code>, which uses <code>fetch()<\/code> to make a GET request to our API endpoint. We then parse the JSON response using <code>response.json()<\/code>, and set the <code>data<\/code> state variable using <code>setData()<\/code>.<\/p>\n<p>In the <code>return<\/code> statement, we use <code>data.map()<\/code> to render each item in the <code>data<\/code> array as a <code>&lt;div&gt;<\/code> element with the item&#8217;s <code>title<\/code> property as its content.<\/p>\n<p>Note that in a real application, you would probably want to handle errors and loading states, and possibly also use a library like <code>axios<\/code> instead of <code>fetch()<\/code> to simplify your API calls.<\/p>\n<p><button><br \/>\n   <svg viewbox=\"\">\n    <path><\/path>\n   <\/svg><\/button><button><br \/>\n   <svg viewbox=\"\">\n    <path><\/path>\n   <\/svg><\/button><\/p>\n<section><\/section>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Learning Goal: I&#8217;m working on a computer systems and need to help me learn. here&#8217;s an example React code that uses fetch() combined with useEffect() to load data from a REST API and display it on the page::- import React, { useState, useEffect } from &#8220;react&#8221;; function App() { const [data, setData] = useState([]); useEffect(() [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":[],"disciplines":[906],"paper_types":[],"tagged":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/questions\/6028"}],"collection":[{"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/questions"}],"about":[{"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/types\/questions"}],"author":[{"embeddable":true,"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/comments?post=6028"}],"version-history":[{"count":0,"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/questions\/6028\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/media?parent=6028"}],"wp:term":[{"taxonomy":"disciplines","embeddable":true,"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/disciplines?post=6028"},{"taxonomy":"paper_types","embeddable":true,"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/paper_types?post=6028"},{"taxonomy":"tagged","embeddable":true,"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/tagged?post=6028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}