Skip to content

Picker Refactor #1 #1000

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

Merged
merged 7 commits into from
Nov 8, 2020
Merged

Picker Refactor #1 #1000

merged 7 commits into from
Nov 8, 2020

Conversation

ethanshar
Copy link
Collaborator

@ethanshar ethanshar commented Oct 25, 2020

Description

Step 1 - Migrate PickerItem from Class to a Function component.

This in order to start using Context for communicating between the Picker parent component and its children (PickerItems)
Once PickerItem is a Function component I'll be able to use hooks and specifically the useContext hook.

Changelog

Refactor PicketItem component


const renderSelectedIndicator = () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe it's better to useMemo in here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done


const _renderItem = () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

also in here, should be memoized

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't want to overkill the component with memoization.
I read in a blog post about when to useMemo and it says it's not recommended to overuse memoization cause it the it can also affect performance.
From what I read, the thumb rule is to start simple and to consider memoizing when we face performance issues.

memoizing this will require wrapping getLabel with useCallback and I prefer not to complicate it at the moment.

Anyway, Personally I still not certain what is best practice with how often to use useMemo so again, for now I prefer to go easy on it..

Copy link
Contributor

Choose a reason for hiding this comment

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

Gotcha. confusing topic indeed :)

* Callback for onLayout event
*/
onSelectedLayout: PropTypes.func
};

export default PickerItem;
Copy link
Contributor

Choose a reason for hiding this comment

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

PickerItem should be exported as memoized component

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Due tho some of the of the props in PickerItem (like renderItem callback prop), it's very possible that memoizing the whole component might cause some side effects and break behaviors.
For now I think it will be better to keep parity with the previous implementation (where PickerItem was simple Component and not a pure one) and later on consider optimizations if necessary.

Copy link
Contributor

Choose a reason for hiding this comment

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

Understood :)

const {value, label} = this.props;
const onSelectedLayout = useCallback((...args) => {
_.invoke(props, 'onSelectedLayout', ...args);
}, []);
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't we pass props as a dependency?

Copy link
Collaborator Author

@ethanshar ethanshar Nov 5, 2020

Choose a reason for hiding this comment

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

this is internal usage.
onSelectedLayout is being sent from the Picker and it doesn't change..
we need it for first mount, so dependencies are irrelevant in this case..

generateStyles() {
this.styles = createStyles(this.props);
}
// TODO: deprecate the check for object
Copy link
Contributor

Choose a reason for hiding this comment

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

TODO hints for isObject check, but in onPress it's commented out

Copy link
Collaborator Author

@ethanshar ethanshar Nov 5, 2020

Choose a reason for hiding this comment

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

I removed it in a following PR

console.warn('UILib Picker.Item will stop supporting passing object as value & label (e.g {value, label}) in the next major version. Please pass separate label and value props');
}
}

}, []);
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a lint error,
We should either pass value as a dependency or no square brackets to trigger on each render

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@ethanshar ethanshar requested a review from mendyEdri November 5, 2020 16:40
Copy link
Contributor

@mendyEdri mendyEdri left a comment

Choose a reason for hiding this comment

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

I've learned few things from your comments 👏

@mendyEdri mendyEdri merged commit 90da8c3 into master Nov 8, 2020
@ethanshar ethanshar deleted the infra/PickerRefactor_#1 branch March 8, 2021 09:02
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.

2 participants