I am running into a performance issue that I don't know how to solve.
I have a component structure like so (<PostDetail>
being the parent component):
Data in the post
state that is passed through the PostContext
(see below)
{
"_id": "5eb1a5dd53ec08cdabca7ef3",
[... more data]
"description": "{blocks: Array(5), entityMap: {…}}",
"likeCount": 6,
"comments": "[{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, …]"
isLikedByUser: true
}
On the following screen, you can see the chrome performance tab when I click on the "Like" button of my component, that updates the likeCount
, which is only displayed in the <LikeCount>
component.
When I click on the "Like" button, I send standard Axios post request and then, I update my post
state that is passed through the PostContext
using :
import { PostContext } from "../../../context/PostContext"
const [post, setPost] = useContext(PostContext)
[...]
setPost({ ...post, isLikedByUser: isLiked, likeCount: likeCount })
Nothing fancy here but, as you can see on the previous screenshot, the whole update takes about 300ms (it's even worse on a smartphone...), which is very bad for the user experience.
What can I do to only update the <LikeCount>
, and not the other components inside <PostDetail>
(<Description>
or <Comments>
for instance)