Created
April 2, 2022 13:26
-
-
Save pushkar100/5da4b69cffd04b0f879c50de1fbf729a to your computer and use it in GitHub Desktop.
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 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