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

Improve memoization of LHN by handling the isFocused prop better #28615

Merged
4 changes: 2 additions & 2 deletions src/components/LHNOptionsList/LHNOptionsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {FlatList, View} from 'react-native';
import _ from 'underscore';
import CONST from '../../CONST';
import styles from '../../styles/styles';
import OptionRowLHNData from './OptionRowLHNData';
import variables from '../../styles/variables';
import OptionRowLHNDataWithFocus from './OptionRowLHNDataWithFocus';

const propTypes = {
/** Wrapper style for the section list */
Expand Down Expand Up @@ -63,7 +63,7 @@ function LHNOptionsList({style, contentContainerStyles, data, onSelectRow, optio
* @return {Component}
*/
const renderItem = ({item}) => (
<OptionRowLHNData
<OptionRowLHNDataWithFocus
reportID={item}
viewMode={optionMode}
shouldDisableFocusOptions={shouldDisableFocusOptions}
Expand Down
15 changes: 4 additions & 11 deletions src/components/LHNOptionsList/OptionRowLHNData.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {withReportCommentDrafts} from '../OnyxProvider';
import SidebarUtils from '../../libs/SidebarUtils';
import compose from '../../libs/compose';
import ONYXKEYS from '../../ONYXKEYS';
import withCurrentReportID, {withCurrentReportIDPropTypes, withCurrentReportIDDefaultProps} from '../withCurrentReportID';
import OptionRowLHN, {propTypes as basePropTypes, defaultProps as baseDefaultProps} from './OptionRowLHN';
import * as Report from '../../libs/actions/Report';
import * as UserUtils from '../../libs/UserUtils';
Expand All @@ -20,8 +19,8 @@ import CONST from '../../CONST';
import reportActionPropTypes from '../../pages/home/report/reportActionPropTypes';

const propTypes = {
/** If true will disable ever setting the OptionRowLHN to focused */
shouldDisableFocusOptions: PropTypes.bool,
/** Wether row should be focused */
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/** Wether row should be focused */
/** Whether row should be focused */

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 ✅

isFocused: PropTypes.bool,

/** List of users' personal details */
personalDetails: PropTypes.objectOf(participantPropTypes),
Expand Down Expand Up @@ -51,20 +50,17 @@ const propTypes = {
/** The ID of the transaction */
transactionID: PropTypes.string,
}),

...withCurrentReportIDPropTypes,
...basePropTypes,
};

const defaultProps = {
shouldDisableFocusOptions: false,
isFocused: false,
personalDetails: {},
fullReport: {},
policy: {},
parentReportActions: {},
transaction: {},
preferredLocale: CONST.LOCALES.DEFAULT,
...withCurrentReportIDDefaultProps,
...baseDefaultProps,
};

Expand All @@ -75,8 +71,7 @@ const defaultProps = {
* re-render if the data really changed.
*/
function OptionRowLHNData({
shouldDisableFocusOptions,
currentReportID,
isFocused,
fullReport,
reportActions,
personalDetails,
Expand All @@ -91,7 +86,6 @@ function OptionRowLHNData({
const reportID = propsToForward.reportID;
// We only want to pass a boolean to the memoized component,
// instead of a changing number (so we prevent unnecessary re-renders).
const isFocused = !shouldDisableFocusOptions && currentReportID === reportID;
Copy link
Contributor

Choose a reason for hiding this comment

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

We can move this comment to OptionRowLHNDataWithFocus, since we moved the isFocused there as well.

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 ✅


const parentReportAction = parentReportActions[fullReport.parentReportActionID];

Expand Down Expand Up @@ -172,7 +166,6 @@ const personalDetailsSelector = (personalDetails) =>
*/
export default React.memo(
compose(
withCurrentReportID,
withReportCommentDrafts({
propName: 'comment',
transformValue: (drafts, props) => {
Expand Down
32 changes: 32 additions & 0 deletions src/components/LHNOptionsList/OptionRowLHNDataWithFocus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import PropTypes from 'prop-types';
import withCurrentReportID, {withCurrentReportIDPropTypes, withCurrentReportIDDefaultProps} from '../withCurrentReportID';
import OptionRowLHNData from './OptionRowLHNData';

const propTypes = {
...withCurrentReportIDPropTypes,
shouldDisableFocusOptions: PropTypes.bool,
};

const defaultProps = {
...withCurrentReportIDDefaultProps,
shouldDisableFocusOptions: false,
};

function OptionRowLHNDataWithFocus({currentReportID, shouldDisableFocusOptions, ...props}) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add some comments stating what this OptionRowLHNDataWithFocus does? It would be cool in future to have an understanding of what it does because not everyone will be coming back to the PR to understand.

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 ✅

const isFocused = !shouldDisableFocusOptions && currentReportID === props.reportID;

return (
<OptionRowLHNData
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
isFocused={isFocused}
/>
);
}

OptionRowLHNDataWithFocus.defaultProps = defaultProps;
OptionRowLHNDataWithFocus.propTypes = propTypes;
OptionRowLHNDataWithFocus.displayName = 'OptionRowLHNDataWithFocus';

export default withCurrentReportID(OptionRowLHNDataWithFocus);
Loading