-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
[Idea: ScrollView] Add getScrollResponder
to ScrollView for composition
#766
Conversation
I think I need a little more context about how this is used. Do you have an example implementation you can point me to? |
I'll start with some background to paint a complete picture: I'm building "higher-order" scroll views that have some extra functionality. For example, an infinite scroll view invokes a callback to fetch more data when the scroll position approaches the end. This could look like: class InfiniteScrollView extends React.Component {
scrollTo(destY, destX) {
this.refs.scrollView.scrollTo(destY, destX);
}
render() {
return (
<ScrollView
{...this.props}
ref="scrollView"
onScroll={(event) => {
if (this._shouldLoadMoreContent(event)) {
this.props.onLoadMore();
}
}}>
{this.props.children}
</ScrollView>
);
}
}
// * this could be a mixin, but there are other advanced scroll views that require
// a new component class Now I'd like to use a different kind of scroll view, like a ListView, ReversedScrollView, etc. so I add a prop like The one snag is implementing methods like
I prefer the first approach because if ScrollView's methods are added/removed/renamed, the code for all other scrollable components doesn't need to be updated. It's a little more verbose with the ScrollableMixin = {
scrollTo(destY, destX) {
invariant(this.getScrollResponder, 'Scrollable components must implement getScrollResponder');
this.getScrollResponder().scrollTo(destY, destX);
}
}; |
Thank you for the detailed explanation. That makes a lot of sense to me. @vjeux? |
I've been meaning to do something similar for a while in order to support both iOS and Android specific list views but a single ListView/InfiniteScrollView/RefreshableScrollView... We've been moving away from passing a class as props to passing a function. This way the call site can pass props and the implementation can cloneElement to pass props as well.
|
@vjeux good idea with the function. Just wondering, is there a reason the prop takes a function instead of a component instance directly? Maybe how the ref owner is determined? |
More about future proofing the api, i can almost guarantee you that you're going to want to pass arguments computed from within the extended scroll view component at some point |
Thought about it some more - the function also lets us do nested composition e.g. <ScrollViewA
renderScrollView={() =>
<ScrollViewB renderScrollView={() => <ScrollViewC/ >} />
}
/> |
6286480
to
bf524ee
Compare
bc49683
to
a02056f
Compare
This is a proposal to add `getScrollResponder` to all ScrollView-like components, including ListView. This allows multiple higher-order scroll views to be composed while allowing the owner of the top-level scroll view to call `scrollableView.getScrollResponder().scrollTo(...)` regardless of whether `scrollableView` is a ScrollView, ListView, InvertedScrollView, etc.
getScrollResponder
to ScrollView for compositiongetScrollResponder
to ScrollView for composition
Oops, wrong PR. |
@facebook-github-bot import |
Thanks for importing. If you are an FB employee go to https://our.intern.facebook.com/intern/opensource/github/pull_request/622194451249017/int_phab to review. |
…tion Summary: This is a proposal to add `getScrollResponder` to all ScrollView-like components, including ListView. This allows multiple higher-order scroll views to be composed while allowing the owner of the top-level scroll view to call `scrollableView.getScrollResponder().scrollTo(...)` regardless of whether `scrollableView` is a ScrollView, ListView, InvertedScrollView, etc. Closes facebook#766 Github Author: James Ide <[email protected]> Test Plan: Imported from GitHub, without a `Test Plan:` line.
…tion Summary: This is a proposal to add `getScrollResponder` to all ScrollView-like components, including ListView. This allows multiple higher-order scroll views to be composed while allowing the owner of the top-level scroll view to call `scrollableView.getScrollResponder().scrollTo(...)` regardless of whether `scrollableView` is a ScrollView, ListView, InvertedScrollView, etc. Closes facebook#766 Github Author: James Ide <[email protected]> Test Plan: Imported from GitHub, without a `Test Plan:` line.
Ran into this problem while following this guide verbatim.
This is a proposal to add
getScrollResponder
to all ScrollView-like components, including ListView. This allows multiple higher-order scroll views to be composed while allowing the owner of the top-level scroll view to callscrollableView.getScrollResponder().scrollTo(...)
regardless of whetherscrollableView
is a ScrollView, ListView, InvertedScrollView, etc.