diff --git a/02_lesson_starter/src/features/posts/PostAuthor.js b/02_lesson_starter/src/features/posts/PostAuthor.js new file mode 100644 index 0000000..4d188e7 --- /dev/null +++ b/02_lesson_starter/src/features/posts/PostAuthor.js @@ -0,0 +1,10 @@ +import { useSelector } from "react-redux"; +import { selectAllUsers } from "../users/usersSlice"; + +const PostAuthor = ({ userId }) => { + const users = useSelector(selectAllUsers); + const author = users.find((user) => user.id === userId); + return by {author ? author.name : "Unknown author"}; +}; + +export default PostAuthor; diff --git a/02_lesson_starter/src/features/posts/PostsList.js b/02_lesson_starter/src/features/posts/PostsList.js index 36dd6be..67964d9 100644 --- a/02_lesson_starter/src/features/posts/PostsList.js +++ b/02_lesson_starter/src/features/posts/PostsList.js @@ -1,5 +1,6 @@ import { useSelector } from "react-redux"; import { selectAllPosts } from "./postsSlice"; +import PostAuthor from "./PostAuthor"; const PostsList = () => { const posts = useSelector(selectAllPosts); @@ -7,8 +8,10 @@ const PostsList = () => { const renderedPosts = posts.map((post) => (

{post.title}

-

{post.userId}

{post.content.substring(0, 100)}

+

+ +

));