You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I also got this issue. @promer94 What do you think should be the correct behavior of mutate/trigger?
I suggest it always return the fetcher's result, undefined if fetcher isn't provided, because sometimes we have to mutate local data in a chain.
For example, in an IM app, when user sends a new message to a conversation, we need to insert this message to local message list and update the corresponding conversation's latest message.
I'm not sure if the following is the best practice, anyway it's our current way:
exportfunctionuseSendMessage(conversationId:string,message: string){constinsertMessage: ()=>Promise<Message>=()=>{// promise that insert message through a post request}const{trigger: triggerMessages}=useSWRMutation<Message[]>(MESSAGGS_KEY_OF(conversationId),post,{populateCache(result,currentData)=>{// the first issue here, type mismatched, // mutate/trigger expects the fetcher's response type the same as the existing data,// which is not always trueconstinsertedMessage=resultasunknownasMessagereturn[message, ...currentData]}})returnuseSWRMutation<Conversation[]>(CONVERSATIONS_KEY,()=>triggerMessages(),{populacateCache(result,currentData)=>{// the second issue here, the triggerMessages() sometimes returns Message, // sometimes returns Message[], depending on values of populateCache and revalidateconstinsertedMessage=result[0]// in some other case, it should be // const insertedMessage = result// update conversations then return}})}
Problem
The runtime behavior of
mutate/trigger
is complex and the current type does not correctly match the actual behavior.related issues
#2647 #2661
The text was updated successfully, but these errors were encountered: