WhenVisible component as a convenient way to load data when an element becomes visible in the viewport.
The WhenVisible component accepts a data prop that specifies the key of the prop to load. It also accepts a fallback prop that specifies a component to render while the data is loading. The WhenVisible component should wrap the component that depends on the data.
data prop.
Loading Before Visible
If you’d like to start loading data before the element is visible, you can provide a value to thebuffer prop. The buffer value is a number that represents the number of pixels before the element is visible.
WhenVisible component wraps the fallback template in a div element so it can ensure the element is visible in the viewport. If you want to customize the wrapper element, you can provide the as prop.
Always Trigger
By default, theWhenVisible component will only trigger once when the element becomes visible. If you want to always trigger the data loading when the element is visible, you can provide the alwaysprop.
This is useful when you want to load data every time the element becomes visible, such as when the element is at the end of an infinite scroll list and you want to load more data. Alternatively, you can use the Infinite scroll component which handles this use case for you.
Note that if the data loading request is already in flight, the component will wait until it is finished to start the next request if the element is still visible in the viewport.
Form Submissions
When submitting forms, you may want to use theexcept option to exclude the props that are being used by the WhenVisible component. This prevents the props from being reloaded when you get redirected back to the current page because of validation errors.