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

migrated ScrollViewWithContext to a functional component #23879

Merged
Changes from 1 commit
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
100 changes: 43 additions & 57 deletions src/components/ScrollViewWithContext.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,54 @@
import React from 'react';
import {ScrollView} from 'react-native';
import React, { useState } from 'react';
import { ScrollView } from 'react-native';

const MIN_SMOOTH_SCROLL_EVENT_THROTTLE = 16;

const ScrollContext = React.createContext();
const defaultScrollViewRef = React.createRef();

// eslint-disable-next-line react/forbid-foreign-prop-types
const propTypes = ScrollView.propTypes;

/*
* <ScrollViewWithContext /> is a wrapper around <ScrollView /> that provides a ref to the <ScrollView />.
* <ScrollViewWithContext /> can be used as a direct replacement for <ScrollView />
* if it contains one or more <Picker /> / <RNPickerSelect /> components.
* Using this wrapper will automatically handle scrolling to the picker's <TextInput />
* when the picker modal is opened
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add the comment back?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

class ScrollViewWithContext extends React.Component {
constructor(props) {
super(props);

this.state = {
contentOffsetY: 0,
};
this.scrollViewRef = this.props.innerRef || React.createRef(null);

this.setContextScrollPosition = this.setContextScrollPosition.bind(this);
}

setContextScrollPosition(event) {
if (this.props.onScroll) {
this.props.onScroll(event);
}
this.setState({contentOffsetY: event.nativeEvent.contentOffset.y});
}

render() {
return (
<ScrollView
// eslint-disable-next-line react/jsx-props-no-spreading
{...this.props}
ref={this.scrollViewRef}
onScroll={this.setContextScrollPosition}
scrollEventThrottle={this.props.scrollEventThrottle || MIN_SMOOTH_SCROLL_EVENT_THROTTLE}
>
<ScrollContext.Provider
value={{
scrollViewRef: this.scrollViewRef,
contentOffsetY: this.state.contentOffsetY,
}}
>
{this.props.children}
</ScrollContext.Provider>
</ScrollView>
);
}
}
ScrollViewWithContext.propTypes = propTypes;
const ScrollViewWithContext = React.forwardRef(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write this as function ScrollViewWithContext and we don't need React.forwardRef we already forward the ref in the exported function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

({ onScroll, scrollEventThrottle, children, innerRef, ...restProps }, ref) => {
const [contentOffsetY, setContentOffsetY] = useState(0);
const scrollViewRef = innerRef || ref || defaultScrollViewRef;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the fallback ref we should use useRef

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. can't add directly useRef(null) in line 15 in place of defaultScrollViewRef because it throughs error that useRef cannot be called conditionally.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we still have innerRef and ref? Shouldn't it be just innerRef?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed extra ref


const setContextScrollPosition = (event) => {
if (onScroll) {
onScroll(event);
}
setContentOffsetY(event.nativeEvent.contentOffset.y);
};

return (
<ScrollView
ref={scrollViewRef}
// eslint-disable-next-line react/jsx-props-no-spreading
{...restProps}
onScroll={setContextScrollPosition}
scrollEventThrottle={scrollEventThrottle || MIN_SMOOTH_SCROLL_EVENT_THROTTLE}
>
<ScrollContext.Provider
value={{
scrollViewRef,
contentOffsetY,
}}
>
{children}
</ScrollContext.Provider>
</ScrollView>
);
}
);

ScrollViewWithContext.propTypes = propTypes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add displayName

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


export default React.forwardRef((props, ref) => (
<ScrollViewWithContext
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
innerRef={ref}
/>
<ScrollViewWithContext
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
innerRef={ref}
/>
));

export {ScrollContext};
export { ScrollContext };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing new line. Please run prettier (npm run prettier)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done