fetch api map code example
Example 1: fetch api javascript
// This will fetch api.example.com/comments with a header and a body
fetch(`https://api.example.com/comments`, {
method: 'POST', //This could be any http method
headers: {
'Authorization': 'Basic SGVsbG8gdGhlcmUgOikgSGF2ZSBhIGdvb2QgZGF5IQ==',
'Content-Type': 'application/json',
},
body: JSON.stringify({
UID: 58,
Comment: "Fetch is really easy!",
}),
})
.then((response) => response.json)
.then((newComment) => {
// Do something magical with your newly posted comment :)
});
Example 2: fetch api map
const GetData = [];
useEffect(() => {
fetch(API_URL)
.then((res) => res.json())
.then((data) => {
GetModesData.push(...data);
setDataState(GetData.map((d) => d.modeName));
});
}, []);