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

Feat/with scroll enabler #807

Merged
merged 16 commits into from
Jun 24, 2020
Merged

Feat/with scroll enabler #807

merged 16 commits into from
Jun 24, 2020

Conversation

M-i-k-e-l
Copy link
Contributor

There are still a few TS errors that I'm not sure how to solve.

demo/src/screens/commonScreens/index.js Outdated Show resolved Hide resolved
src/commons/withScrollEnabler.tsx Outdated Show resolved Hide resolved

const scrollViewRef = useRef<WithScrollEnablerProps>(null);

useEffect(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do u need an access to the Scroll element ref?

Copy link
Contributor Author

@M-i-k-e-l M-i-k-e-l Jun 15, 2020

Choose a reason for hiding this comment

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

Removed

return (
<WrappedComponent
{...props}
onContentSizeChange={onContentSizeChange}
Copy link
Collaborator

Choose a reason for hiding this comment

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

There might be a little risk with passing the Scroll props as is. with the same names.
They can be passed in a spread by mistake to places you don't want. also I think we might want to have a more explicit name for them so the user should understand where they are coming from .
Another option is to group them as an object that the user can spread intentionally

for instance

<WrappedComponent scrollEnablerProps={{onLayout, scrollEnabled, onContentSizeChange}}

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, I've created two use classes, 1 for FlatList and 1 for ListView, they are very similar but I'm not sure how easy they are to combine.

@M-i-k-e-l M-i-k-e-l requested a review from ethanshar June 15, 2020 11:27
Copy link
Collaborator

@ethanshar ethanshar left a comment

Choose a reason for hiding this comment

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

Overall the component looks good.
I noticed that there are some mis-usages of useCallback and in general how you handle function inside a function component

I suggest going over this list of common hooks
https://reactjs.org/docs/hooks-reference.html#useimperativehandle

setContentSize(size);
}
},
[props]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a reason to pass all props in the dependency array?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed and removed - does not need to use state here

},
[props]);
},
[props]
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here?
why do u pass all the props?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed and removed - does not need to use state here


const WithScrollEnabler = withScrollEnabler(
(props: WithScrollEnablerProps) => {
function renderItem(index: number) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You must use useCallback in order to define methods inside a function component.
Otherwise what will happen is that on every render you will re-create those functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to useCallback


const WithScrollEnabler = withScrollEnabler(
(props: WithScrollEnablerProps) => {
const getData = memoize((numberOfItems: number) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

even though you memoize this function you still re-creating it on each render.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to useCallback

@M-i-k-e-l M-i-k-e-l requested a review from ethanshar June 15, 2020 15:27
# Conflicts:
#	generatedTypes/index.d.ts
if (isScrollEnabled !== scrollEnabled) {
setScrollEnabled(isScrollEnabled);
}
}, [contentSize, layoutSize]);
}, []);
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you're using something inside the callback it should be part of the dependencies array of the useCallback
in this case you're using scrollEnabled state, so it should be included in the array.

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

@M-i-k-e-l M-i-k-e-l requested a review from ethanshar June 17, 2020 15:09
@@ -42,7 +42,7 @@ function withScrollEnabler<PROPS extends SupportedViews>(
}
}
},
[props.horizontal]
[props.horizontal, checkScroll]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think it's recommended to pass to the dependency array a callback.
and actually I don't think you even need to, I think [props.horizontal] is enough in this case.

same goes for the onLayout callback

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was recommended by the new plugin.

@ethanshar ethanshar merged commit d83cc6c into master Jun 24, 2020
@M-i-k-e-l M-i-k-e-l deleted the feat/with-scroll-enabler branch September 21, 2020 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants