-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotes_Doubt_Session_2-7-24.txt
More file actions
147 lines (107 loc) · 3.65 KB
/
Copy pathNotes_Doubt_Session_2-7-24.txt
File metadata and controls
147 lines (107 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
Sync Functions:
Lets say that we have to fetch an API, it might take some time right ?
so if we use a sync function, it won't wait for the fetching to be completed.
it will straight away just run till the end without pausing.
Without await: You might run around the house, doing other things, and check the pizza only when you remember.
With await: You just wait right there until the pizza is ready, and then you get it.
import React, { useState } from "react";
function Sync(){
const [syncmessage, setSyncmessage] = useState("")
const handlesync = () =>{
setSyncmessage("Synchrnous Function Exected !!")
console.log(syncmessage)
}
return(
<>
<h1>Sync Function in React :</h1>
<button onClick={handlesync}>Run SYNC Function</button>
</>
);
}
export default Sync;
import React, { useState, useEffect } from 'react';
function SimpleFetchExample() {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
// Define an async function to fetch data
const fetchData = async () => {
// Use await to wait for the fetch call to complete
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1');
// Use await to wait for the response to be converted to JSON
const result = await response.json();
// Update the state with the fetched data
setData(result);
// Indicate that loading is complete
setLoading(false);
};
// Call the async function
fetchData();
}, []); // Empty dependency array means this effect runs only once when the component mounts
return (
<div>
<h1>Fetched Data</h1>
{data && (
<div>
<p>ID: {data.id}</p>
<p>Title: {data.title}</p>
</div>
)}
</div>
);
}
export default SimpleFetchExample;
//BEST_EXAMPLE
import React, { useEffect, useState } from 'react';
function Asyncc() {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(()=>{
async function fetchd(){
const response = await fetch('https://pokeapi.co/api/v2/pokemon/3')
const result = await response.json()
setData(result)
setLoading(false)
}
fetchd();
}, [])
return (
<>
<div>
<h1>{data.name}</h1>
<img src={data.sprites.front_default}></img>
</div>
</>
);
}
export default Asyncc;
import React, { useEffect, useState } from "react";
function Async(){
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(()=>{
async function fetchdata(){
const resp = await fetch('https://rickandmortyapi.com/api/character');
const result = await resp.json();
setData(result);
setLoading(false);
}
fetchdata();
}, [])
if (loading) {
return <div>Loading...</div>;
}
return(
<>
<div>
<h1>Character :</h1>
<ul>{data.results.map((charac, index)=>(
<li key={index}>
<div>{charac.id}. {charac.name}</div>
</li>
))}</ul>
</div>
</>
);
}
export default Async;