Skip to content

Instantly share code, notes, and snippets.

@pushkar100
Last active May 1, 2022 20:15
Show Gist options
  • Select an option

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

Select an option

Save pushkar100/a9af291de92192507dab80ac7ec312d3 to your computer and use it in GitHub Desktop.
import React, { useState, useCallback } from "react";
const readStyle = { backgroundColor: "grey" };
const unreadStyle = { backgroundColor: "white" };
const EmailsListItem = ({ hasRead, onReadStatusChange }) => {
const [read, setRead] = useState(hasRead || false);
const resetRead = useCallback(() => {
setRead(hasRead);
// Handle side effects during a reset!
if (onReadStatusChange) {
onReadStatusChange(hasRead);
}
}, [hasRead]);
const style = read ? readStyle : unreadStyle;
return (
<div style={style}>
<div>Subject: Email one, Content: Hello</div>
<button
onClick={() => {
/* Renders email content */
setRead(true);
}}
>
Open
</button>
<button onClick={() => resetRead()}>Reset read</button>
</div>
);
};
export default EmailsListItem;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment