en nice
en nice

Reputation: 31

how update state in rtk query by useEffect

i have two endpoints when i add new value to endpoint one i want endpoint two updated by useEffect Hook

const PostDetails = (props) => {
   const id = props.match.params.id;

   const [addNewComment, { isSuccess: success, isError }] = useAddNewCommentMutation();

 

   const dispatch = useDispatch();

   const selectPost = useMemo(() => postApi.endpoints.getPost.select(id), [id]);
   const { data: post, isLoading } = useSelector(selectPost);

   useEffect(() => {
      const result = dispatch(postApi.endpoints.getPost.initiate(id));

      return result.unsubscribe;
   }, [id, dispatch, success]);

it said name(pin):"ConditionError" message(pin):"Aborted due to condition callback returning false."

Upvotes: 2

Views: 4807

Answers (1)

phry
phry

Reputation: 44086

That just means "there is already data and I have no reason to assume it is outdated, I'm not gonna fetch again".

You can do a

dispatch(postApi.endpoints.getPost.initiate(id, {forceRefetch: true}));

but that's not what you should do here.

What you really should use this is the invalidation feature.

So your endpoint getPost has a providesTags function that returns [{ type: 'Post', id: 5 }] and your addNewComment mutation has an invalidatesTags function that returns [{ type: 'Post', id: 5 }], too.

That way, whenever you call that addNewComment mutation, the getPost endpoint will refetch.

Please read the documentation chapter about Automated Refetching

Upvotes: 2

Related Questions