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 #2048

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/react/src/WhenVisible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ const WhenVisible = ({ children, data, params, buffer, as, always, fallback }: W
const ref = useRef<HTMLDivElement>(null)

const getReloadParams = useCallback<() => Partial<ReloadOptions>>(() => {
if (data) {
return {
only: (Array.isArray(data) ? data : [data]) as string[],
if (!params && !data) {
throw new Error('You must provide either a `data` or `params` prop.')
}
}

if (!params) {
throw new Error('You must provide either a `data` or `params` prop.')
}
const reloadParams: Partial<ReloadOptions> = params || {} ;

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

return params
return reloadParams;
}, [params, data])

useEffect(() => {
Expand Down
14 changes: 7 additions & 7 deletions packages/svelte/src/components/WhenVisible.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@
})

function getReloadParams(): Partial<ReloadOptions> {
if (data !== '') {
return {
only: (Array.isArray(data) ? data : [data]) as string[],
}
if (!params && !data) {
throw new Error('You must provide either a `data` or `params` prop.')
}

if (!params.data) {
throw new Error('You must provide either a `data` or `params` prop.')
const reloadParams: Partial<ReloadOptions> = params || {} ;

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

return params
return reloadParams;
}
</script>

Expand Down
16 changes: 9 additions & 7 deletions packages/vue3/src/whenVisible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,19 @@ export default defineComponent({
},
methods: {
getReloadParams(): Partial<ReloadOptions> {
if (this.$props.data) {
return {
only: (Array.isArray(this.$props.data) ? this.$props.data : [this.$props.data]) as string[],
}
}
const { params, data } = this.$props;

if (!this.$props.params) {
if (!params && !data) {
throw new Error('You must provide either a `data` or `params` prop.')
}

return this.$props.params
const reloadParams: Partial<ReloadOptions> = params || {} ;

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

return reloadParams;
},
},
render() {
Expand Down
Loading