Skip to content

Commit

Permalink
refactor getReloadParams to a cleaner way
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroAugustoRamalhoDuarte committed Oct 20, 2024
1 parent 8ce1fbc commit c1df10a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
9 changes: 6 additions & 3 deletions packages/react/src/WhenVisible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ const WhenVisible = ({ children, data, params, buffer, as, always, fallback }: W
throw new Error('You must provide either a `data` or `params` prop.')
}

return {
...params,
...(data && { only: Array.isArray(data) ? data : [data] }),
const reloadParams: Partial<ReloadOptions> = params || {} ;

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

return reloadParams;
}

useEffect(() => {
Expand Down
9 changes: 6 additions & 3 deletions packages/svelte/src/components/WhenVisible.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,13 @@
throw new Error('You must provide either a `data` or `params` prop.')
}
return {
...params,
...(data && { only: Array.isArray(data) ? data : [data] }),
const reloadParams: Partial<ReloadOptions> = params || {} ;
if (data) {
reloadParams.only = (Array.isArray(data) ? data : [data]) as string[];
}
return reloadParams;
}
</script>

Expand Down
13 changes: 9 additions & 4 deletions packages/vue3/src/whenVisible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,19 @@ export default defineComponent({
},
methods: {
getReloadParams(): Partial<ReloadOptions> {
if (!this.$props.params && !this.$props.data) {
const { params, data } = this.$props;

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

return {
...this.$props.params,
...(this.$props.data && { only: Array.isArray(this.$props.data) ? this.$props.data : [this.$props.data] }),
const reloadParams: Partial<ReloadOptions> = params || {} ;

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

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

0 comments on commit c1df10a

Please sign in to comment.