jsx
import React, { useState, useEffect } from 'react';
const JokeHighlighter = () => {
const [joke, setJoke] = useState('');
const jokes = [
"Why did the coffee file a police report? Because it got mugged!",
"I told my wife she was drawing her eyebrows too high. She looked surprised.",
"Why don't scientists trust atoms? Because they make up everything!",
"I'm afraid for the calendar. Its days are numbered.",
"What do you call a fake noodle? An impasta!"
];
const getRandomJoke = () => {
const randomIndex = Math.floor(Math.random() * jokes.length);
return jokes[randomIndex];
};
const handleNewJoke = () => {
setJoke(getRandomJoke());
};
useEffect(() => {
// Set initial joke when component mounts
setJoke(getRandomJoke());
}, []);
return (
<div style={{
maxWidth: '400px',
margin: '20px auto',
padding: '20px',
backgroundColor: '#f0f0f0',
borderRadius: '10px',
textAlign: 'center'
}}>
<h2>Random Joke Generator</h2>
<div style={{
backgroundColor: '#ffff99',
padding: '15px',
borderRadius: '8px',
fontStyle: 'italic',
boxShadow: '0 4px 6px rgba(0,0,0,0.1)'
}}>
"{joke}"
</div>
<button
onClick={handleNewJoke}
style={{
marginTop: '15px',
padding: '10px 20px',
backgroundColor: '#4CAF50',
color: 'white',
border: 'none',
borderRadius: '5px',
cursor: 'pointer'
}}
>
Get Another Joke
</button>
</div>
);
};
export default JokeHighlighter;