-
Hi team! Is there a way to update query data without triggering an UI update even though that query is active on screen? Seems like an odd request but imagine this:
If user clicks upvote, I wouldn't want to update/refetch/rerender whole list but use local state in single item component because it could be very expensive and wasteful depending on feed complexity. But if user navigates away and comes back, I want upvote count to be accurate (i.e include your +1 in query data). Example code: <Feed>
<SinglePost /> {/* upvote button should rerender only this component */}
<SinglePost />
<SinglePost />
{/* could be hundreds if infinite scroll (and thousands in-memory) */}
</Feed> function SinglePost(props) {
const [tempVotes, setTempVotes] = useState(0);
const votes = props.votes + tempVotes;
function onVote() {
setTempVotes(tempVotes + 1);
// do mutation without triggering parent feed refresh but update query data
}
return <div><button onClick={onVote}>{votes}</button> {props.title}</div>;
} I found |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I don't think so. render optimizations are a different topic. If re-rendering all the posts in the list is too expensive, you'd have to memoize them (React.memo comes to mind). You can also have each |
Beta Was this translation helpful? Give feedback.
-
if you want to have a component to retrieve query data, but not subscribe to it, you can do:
if the component renders for whatever reason, it will get the latest data, otherwise, it won't re-render on it's own. I don't think it would solve your problem though. I would do:
then, in the feed view:
and in SinglePost:
because we then only pass an Actually, you can even also get rid of the React.memo if you have the Feed only subscribe to the ids, because they don't change if you like something, only if you add / delete something:
so now if you update with |
Beta Was this translation helpful? Give feedback.
if you want to have a component to retrieve query data, but not subscribe to it, you can do:
if the component renders for whatever reason, it will get the latest data, otherwise, it won't re-render on it's own. I don't think it would solve your problem though. I would do:
then, in the feed view:
and in SinglePost: