Last active
June 27, 2025 16:00
-
-
Save Klerith/163673dab4944efca9e06af95392a5bc to your computer and use it in GitHub Desktop.
Gist para el ejercicio sobre useOptimistic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from 'react'; | |
interface Comment { | |
id: number; | |
text: string; | |
optimistic?: boolean; | |
} | |
export const InstagromApp = () => { | |
const [comments, setComments] = useState<Comment[]>([ | |
{ id: 1, text: '¡Gran foto!' }, | |
{ id: 2, text: 'Me encanta 🧡' }, | |
]); | |
const handleAddComment = async () => { | |
console.log('Nuevo comentario'); | |
}; | |
return ( | |
<div className="bg-slate-700 h-screen flex flex-col items-center justify-center"> | |
{/* Post de ejemplo */} | |
<div className="flex flex-col items-center justify-center bg-gray-300 rounded-t-3xl p-4 w-[500px]"> | |
<img | |
src="https://images.unsplash.com/photo-1649972904349-6e44c42644a7?w=500&h=500&fit=crop" | |
alt="Instagrom" | |
className="object-cover rounded-xl mb-4" | |
/> | |
<p className="text-black font-bold mb-4"> | |
Mira que interesante esta funcionalidad de la API de React. | |
</p> | |
</div> | |
{/* Comentarios */} | |
<ul className="flex flex-col items-start justify-center bg-gray-300 w-[500px] p-4"> | |
{comments.map((comment) => ( | |
<li key={comment.id} className="flex items-center gap-2 mb-2"> | |
<div className="bg-blue-500 rounded-full w-10 h-10 flex items-center justify-center"> | |
<span className="text-white text-center">A</span> | |
</div> | |
<p className="text-black">{comment.text}</p> | |
{comment.optimistic && ( | |
<span className="text-gray-500 text-sm">enviando... </span> | |
)} | |
</li> | |
))} | |
</ul> | |
{/* Formulario de comentarios */} | |
<form | |
action={handleAddComment} | |
className="flex flex-col items-center justify-center bg-gray-300 w-[500px] rounded-b-3xl p-4" | |
> | |
<input | |
type="text" | |
name="post-message" | |
placeholder="Escribe un comentario" | |
required | |
className="w-full p-2 rounded-md mb-2 text-black bg-white" | |
/> | |
<button | |
type="submit" | |
disabled={false} | |
className="bg-blue-500 text-white p-2 rounded-md w-full" | |
> | |
Enviar | |
</button> | |
</form> | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment