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

[V2] Improve WhenVisible component to merge data and params props for a more flexible API #2025

Open
PedroAugustoRamalhoDuarte opened this issue Oct 12, 2024 · 2 comments · May be fixed by #2048

Comments

@PedroAugustoRamalhoDuarte

Versions:

  • @inertiajs/react version: v2-beta

Describe the problem:

In the current implementation of the WhenVisible component, it is not possible to use both data and params props simultaneously. The issue lies in the getReloadParams function, which either returns data or params but not both. To provide a more flexible and user-friendly API, it would be ideal to merge the data prop into params when both are provided.

Steps to reproduce:

  1. Use the WhenVisible component with both data and params props.
  2. The getReloadParams function will only consider data and ignore params.
<WhenVisible fallback={"Loading..."} data={["todos", "pagy"]} params={{
  data: {
    teste: true,
    page: pagy.page + 1,
  },
  preserveUrl: true,
}} />

Actual code

const getReloadParams = (): Partial<ReloadOptions> => {

Expected behavior:

When both data and params are provided, the function should merge them into a single object, allowing both to work together.

A initial implementation for React can be something like:

const getReloadParams = (): Partial<ReloadOptions> => {
  const reloadParams: Partial<ReloadOptions> = { ...params }

  if (data) {
    reloadParams.only = (Array.isArray(data) ? data : [data]) as string[]
  }

  return reloadParams
}
@shankiflang
Copy link

Hi!
I have the problem too!
I want to set data to my feed and use params to manage the currentPage number update but it doesn't work.

@irsyadadl
Copy link

irsyadadl commented Oct 18, 2024

When using params, the data actually should be optional. Right now it doesn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants