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

feat(useInstantSearch): expose renderState #5873

Merged
merged 1 commit into from
Sep 29, 2023

Conversation

Haroenv
Copy link
Contributor

@Haroenv Haroenv commented Sep 29, 2023

Summary

The renderState is useful for interacting with a different widget that you know / assume is also mounted. This is good for example for refining within a hit component without adding a second refinement widget (which could have side effects if hits is conditional, despite that impact being made smaller through #5123).

Can also be useful for a reusable widget that allows the user to pass their own options to e.g. refinementList, but still expects it to be mounted by the user.

An example use case:

function Hits(props) {
  const { renderState } = useInstantSearch();
  const { hits } = useHits();

  return (
    <ol>
      {hits.map((hit) => (
        <li key={hit.objectID}>
          <button
            type="button"
            onClick={() => {
              indexRenderState.refinementList.keyword.refine(hit.keyword);
            }}
          >
            {hit.keyword}
            {indexRenderState.refinementList.keyword.items.find(
              (item) => item.label === hit.keyword
            )
              ? ' (refined)'
              : ''}
          </button>
        </li>
      ))}
    </ol>
  );
}

Result

RFC-71 is implemented more completely

@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit a3911ae:

Sandbox Source
InstantSearch.js Configuration
react-instantsearch-app Configuration
example-react-instantsearch-default-theme Configuration
example-react-instantsearch-next-app-dir-example Configuration
example-vue-instantsearch-default-theme Configuration

@Haroenv Haroenv marked this pull request as ready for review September 29, 2023 09:30
@Haroenv Haroenv requested review from francoischalifour, a team, sarahdayan and aymeric-giraudet and removed request for a team September 29, 2023 09:30
Copy link
Member

@francoischalifour francoischalifour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if at some point we want to provide render state as a generic to InstantSearch, like UI state and Route state. Currently I don't think there's a way to have a type-safe render state with custom widgets.

@@ -20,6 +22,8 @@ export function useSearchState<
const indexId = searchIndex.getIndexId();
const [uiState, setLocalUiState] = useState(() => search.getUiState());
const indexUiState = uiState[indexId] as TUiState[string];
const [renderState, setRenderState] = useState(() => search.renderState);
const indexRenderState = renderState[indexId] || {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why we have this {} fallback for the index render state but it's not needed for the index UI state?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getUiState uses the mounted index widgets (all) as its start value, but renderState starts with an empty object

@Haroenv
Copy link
Contributor Author

Haroenv commented Sep 29, 2023

Currently I don't think there's a way to have a type-safe render state with custom widgets

You're right, I think it should be reworked, and instead of passing UiState it should be a list of widgets and we can infer all other things from it (uiState, renderState), but not easy to do that backwards compatible / correctly

@Haroenv Haroenv merged commit 9c43382 into master Sep 29, 2023
3 checks passed
@Haroenv Haroenv deleted the feat/use-instantsearch-render-state branch September 29, 2023 11:32
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

Successfully merging this pull request may close these issues.

3 participants