Skip to content

key has change, never revalidate #1531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
j-xzy opened this issue Oct 6, 2021 · 7 comments
Closed

key has change, never revalidate #1531

j-xzy opened this issue Oct 6, 2021 · 7 comments

Comments

@j-xzy
Copy link

j-xzy commented Oct 6, 2021

Bug report

Description / Observed Behavior

version: 1.0.1, key has change, never revalidate

Repro Steps / Code Example

CodeSandbox

Context

#1430 (comment)

@shuding
Copy link
Member

shuding commented Oct 13, 2021

I think this should be the correct behavior of revalidateOnMount: false. When the key changes, it should not automatically revalidate if revalidateOnMount is manually disabled.

@shuding shuding added the discussion Discussion around current or proposed behavior label Oct 13, 2021
@shuding shuding closed this as completed Dec 23, 2021
@GeraldHost
Copy link

@shuding I had a similar issue I want to only revalidate if the key changes but otherwise never revalidate. What combination of options should I use?

1 similar comment
@GeraldHost
Copy link

@shuding I had a similar issue I want to only revalidate if the key changes but otherwise never revalidate. What combination of options should I use?

@filenwind
Copy link

filenwind commented Jan 17, 2022

I have the same problem, I don't want revalidateOnMount but I need revalidateOnKeyChange.

Can you add the new config revalidateOnKeyChange?

More clear, I want using useSWRImmutable and can revalidateOnKeyChange to be true

How can SWR meet my requirement?

Thanks!

I can code like below, but I really want it have officially config to achieve.
if not using useEffect, it won't revalidate if key change back to 'firstKey'

import React, { useEffect, useState, useRef } from "react";
import useSWRImmutable from "swr/immutable";

function fetcher() {
  console.log("revalidate!");
  return 'value';
}

function Test() {
  const [key, setKey] = useState('firstKey');
  const mountedRef = useRef(false);
  const { mutate } = useSWRImmutable([key], fetcher);

  useEffect(() => {
    if (mountedRef.current === false) {
      mountedRef.current = true;
      return;
    }

    mutate();
  }, [mutate, key]);
  return (
    <div>
      <button
        onClick={() => {
          setKey('secondKey');
        }}
      >
        secondKey
      </button>
      <button
        onClick={() => {
          setKey('firstKey');
        }}
      >
        firstKey
      </button>
    </div>
  );
}

@shuding shuding reopened this Jan 20, 2022
@shuding
Copy link
Member

shuding commented Jan 20, 2022

Reopened the issue since the following should be the correct behavior. When the key changes, useSWRImmutable should definitely refetch.

More clear, I want using useSWRImmutable and can revalidateOnKeyChange to be true

@shuding shuding added bug Something isn't working reproduction needed and removed discussion Discussion around current or proposed behavior labels Jan 20, 2022
@caydgreen
Copy link

Is this still an active bug?

@filenwind
Copy link

filenwind commented Mar 21, 2023

here is the reproduce

You can switch key by click each button, and see the revalidate log.

It won't revalidate if key already fetched, so I want a config like revalidateOnKeyChange

I don't know this is a bug or not.

@promer94 promer94 removed bug Something isn't working reproduction needed labels Jun 7, 2023
@promer94 promer94 closed this as completed Jun 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants