I found one API which return a word, definition.
I want to display a word and definition and when the button is pressed it should refetch and show new word.
I wrote this code. But I can't make it work. I'm not sure what I'm doing wrong.
I don't wanna use Axios or any library
import React, { useEffect, useState, Button } from "react";
function RandomWord() {
const [isLoading, setLoading] = useState(true);
const [data, setData] = useState([]);
const [refetch, setRefetch] = useState(false);
useEffect(() => {
fetch("https://rnd-word.vercel.app/word")
.then((response) => response.json())
.then((json) => setData(json))
.catch((error) => console.error(error))
.finally(() => setLoading(false));
}, [refetch]);
return (
<>
<div class="App">
{isLoading ? (
<p>Loading...</p>
) : (
<span
data={data}
keyExtractor={({ id }, index) => id}
renderItem={({ item }) =>
<p>{item.word}</p>
<p>{item.defintion}</p>
}
/>
)}
</div>
<Button
title="New Word"
onPress={() => setRefetch(!refetch)}
color="#e56b6f"
/>
</>
);
}
export default RandomWord;