Skip to content
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

Duplicate requests with instantsearch and useRefinementList hook #5953

Open
1 task done
chrissshh opened this issue Dec 5, 2023 · 1 comment
Open
1 task done

Duplicate requests with instantsearch and useRefinementList hook #5953

chrissshh opened this issue Dec 5, 2023 · 1 comment
Labels
Library: React InstantSearch ≥ 7 Issues in any of the react-instantsearch@7 packages (formerly named react-instantsearch-hooks)

Comments

@chrissshh
Copy link

🐛 Current behavior

Instantsearch sends multiple duplicate requests to fetch results and it seems to happen with this component mostly in instantsearch using the useRefinementList() hook. Does anyone know how to fix this because I've tried just about everything. Thanks!!

function XRefinementList({
  attribute,
}: {
  attribute: string;
}) {
  const { items, refine } = useRefinementList({
    attribute: attribute,
    sortBy: ["count:desc", "name:asc"],
  });

  return (
    <div>
      {items.map((item) => (
        <div key={item.label}>
          <button
            type="button"
            onClick={() => {
              refine(item.value);
            }}
            disabled={item.count === 0}
          >
            {item.label}
          </button>
        </div>
      ))}
    </div>
  );
}

🔍 Steps to reproduce

Add useRefinementList into a component and have multiple components with the instantsearch context. It will perform a fetch for each component with an identical request. After page is loaded, all requests are normal and singular.

Live reproduction

codesandbox.io

💭 Expected behavior

No duplicate requests when loading the page.

Package version

react-instantsearch 7.4

Operating system

No response

Browser

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@chrissshh chrissshh added the triage Issues to be categorized by the team label Dec 5, 2023
@Haroenv
Copy link
Contributor

Haroenv commented Dec 6, 2023

I can think of the props passed to useRefinementList not being stable possibly (although normally we have code to prevent loops), or the component itself not rendering consistently. Do you have a reproducible example on eg. code sandbox?

@Haroenv Haroenv added Library: React InstantSearch ≥ 7 Issues in any of the react-instantsearch@7 packages (formerly named react-instantsearch-hooks) and removed triage Issues to be categorized by the team labels Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Library: React InstantSearch ≥ 7 Issues in any of the react-instantsearch@7 packages (formerly named react-instantsearch-hooks)
Projects
None yet
Development

No branches or pull requests

2 participants