Skip to content

Instantly share code, notes, and snippets.

@pushkar100
Created April 2, 2022 13:26
Show Gist options
  • Select an option

  • Save pushkar100/5da4b69cffd04b0f879c50de1fbf729a to your computer and use it in GitHub Desktop.

Select an option

Save pushkar100/5da4b69cffd04b0f879c50de1fbf729a to your computer and use it in GitHub Desktop.
import React, { useState, useEffect } from "react";
const getMessages = () => [
{ id: 100, text: "Hey", author: "Ram" },
{ id: 101, text: "Hello!", author: "Dennis" },
{ id: 102, text: "How is it going?", author: "Ram" }
];
const useMessages = () => {
const [messages, setMessages] = useState([]);
useEffect(() => {
setMessages(getMessages());
}, []);
return messages;
};
const Messages = () => {
const messages = useMessages();
return (
<ul>
{messages.map(({ id, text, author }) => (
<li key={id}>
{author}: {text}
</li>
))}
</ul>
);
};
export default Messages;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment