// DailyWord Component - Displays word of the day with definition and examples function DailyWord() { const [wordData, setWordData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { fetchDailyWord(); }, []); const fetchDailyWord = async () => { try { setLoading(true); // Using Random Word API + Dictionary API const wordResponse = await fetch('https://random-word-api.herokuapp.com/word'); const words = await wordResponse.json(); const word = words[0]; // Fetch definition const defResponse = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`); const defData = await defResponse.json(); if (defData && defData[0]) { const entry = defData[0]; const meaning = entry.meanings[0]; setWordData({ word: word, phonetic: entry.phonetic || '', partOfSpeech: meaning.partOfSpeech || 'noun', definition: meaning.definitions[0].definition, example: meaning.definitions[0].example || null }); } else { // Fallback word if API fails setWordData({ word: 'serendipity', phonetic: '/ˌserənˈdipitē/', partOfSpeech: 'noun', definition: 'The occurrence and development of events by chance in a happy or beneficial way.', example: 'A fortunate stroke of serendipity brought the two old friends together after years apart.' }); } } catch (err) { console.error('Error fetching daily word:', err); // Set fallback word on error setWordData({ word: 'serendipity', phonetic: '/ˌserənˈdipitē/', partOfSpeech: 'noun', definition: 'The occurrence and development of events by chance in a happy or beneficial way.', example: 'A fortunate stroke of serendipity brought the two old friends together after years apart.' }); } finally { setLoading(false); } }; if (loading) { return (

📚 Daily Word

Loading...

); } if (error || !wordData) { return (

📚 Daily Word

Failed to load word of the day

); } return (

📚 Daily Word

{wordData.word}

{wordData.phonetic && (

{wordData.phonetic}

)}

{wordData.partOfSpeech}

DEFINITION:

{wordData.definition}

{wordData.example && (

EXAMPLE:

"{wordData.example}"

)}
); } // Export to global registry window.DailyWord = DailyWord;