Skip to content

Commit b16d7c1

Browse files
authored
Merge pull request #53323 from callstack-internal/feature/202-eslint-rule-stop-usage-init-function-use-state
Use initializer callbacks for useState
2 parents aa55b4d + 0fcef73 commit b16d7c1

File tree

11 files changed

+16
-28
lines changed

11 files changed

+16
-28
lines changed

src/components/DatePicker/CalendarPicker/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ function CalendarPicker({
5353
const {preferredLocale, translate} = useLocalize();
5454
const pressableRef = useRef<View>(null);
5555

56-
const [currentDateView, setCurrentDateView] = useState(getInitialCurrentDateView(value, minDate, maxDate));
56+
const [currentDateView, setCurrentDateView] = useState(() => getInitialCurrentDateView(value, minDate, maxDate));
5757

5858
const [isYearPickerVisible, setIsYearPickerVisible] = useState(false);
5959

6060
const minYear = getYear(new Date(minDate));
6161
const maxYear = getYear(new Date(maxDate));
6262

63-
const [years, setYears] = useState<CalendarPickerListItem[]>(
63+
const [years, setYears] = useState<CalendarPickerListItem[]>(() =>
6464
Array.from({length: maxYear - minYear + 1}, (v, i) => i + minYear).map((year) => ({
6565
text: year.toString(),
6666
value: year,

src/components/VideoPlayer/BaseVideoPlayer.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function BaseVideoPlayer({
7373
const [isEnded, setIsEnded] = useState(false);
7474
const [isBuffering, setIsBuffering] = useState(true);
7575
// we add "#t=0.001" at the end of the URL to skip first milisecond of the video and always be able to show proper video preview when video is paused at the beginning
76-
const [sourceURL] = useState(VideoUtils.addSkipTimeTagToURL(url.includes('blob:') || url.includes('file:///') ? url : addEncryptedAuthTokenToURL(url), 0.001));
76+
const [sourceURL] = useState(() => VideoUtils.addSkipTimeTagToURL(url.includes('blob:') || url.includes('file:///') ? url : addEncryptedAuthTokenToURL(url), 0.001));
7777
const [isPopoverVisible, setIsPopoverVisible] = useState(false);
7878
const [popoverAnchorPosition, setPopoverAnchorPosition] = useState({horizontal: 0, vertical: 0});
7979
const [controlStatusState, setControlStatusState] = useState(controlsStatus);

src/pages/Debug/DebugDetailsDateTimePickerPage.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function DebugDetailsDateTimePickerPage({
2626
}: DebugDetailsDateTimePickerPageProps) {
2727
const {translate} = useLocalize();
2828
const styles = useThemeStyles();
29-
const [date, setDate] = useState(DateUtils.extractDate(fieldValue));
29+
const [date, setDate] = useState(() => DateUtils.extractDate(fieldValue));
3030
return (
3131
<ScreenWrapper testID={DebugDetailsDateTimePickerPage.displayName}>
3232
<HeaderWithBackButton title={fieldName} />

src/pages/Debug/ReportAction/DebugReportActionCreatePage.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function DebugReportActionCreatePage({
4848
const styles = useThemeStyles();
4949
const [session] = useOnyx(ONYXKEYS.SESSION);
5050
const [personalDetailsList] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST);
51-
const [draftReportAction, setDraftReportAction] = useState<string>(getInitialReportAction(reportID, session, personalDetailsList));
51+
const [draftReportAction, setDraftReportAction] = useState<string>(() => getInitialReportAction(reportID, session, personalDetailsList));
5252
const [error, setError] = useState<string>();
5353

5454
return (

src/pages/Debug/TransactionViolation/DebugTransactionViolationCreatePage.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function DebugTransactionViolationCreatePage({
6363
const {translate} = useLocalize();
6464
const styles = useThemeStyles();
6565
const [transactionViolations] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`);
66-
const [draftTransactionViolation, setDraftTransactionViolation] = useState<string>(getInitialTransactionViolation());
66+
const [draftTransactionViolation, setDraftTransactionViolation] = useState<string>(() => getInitialTransactionViolation());
6767
const [error, setError] = useState<string>();
6868

6969
const editJSON = useCallback(

src/pages/ReimbursementAccount/ReimbursementAccountPage.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ function ReimbursementAccountPage({route, policy}: ReimbursementAccountPageProps
203203
which acts similarly to `componentDidUpdate` when the `reimbursementAccount` dependency changes.
204204
*/
205205
const [hasACHDataBeenLoaded, setHasACHDataBeenLoaded] = useState(reimbursementAccount !== CONST.REIMBURSEMENT_ACCOUNT.DEFAULT_DATA && isPreviousPolicy);
206-
const [shouldShowContinueSetupButton, setShouldShowContinueSetupButton] = useState(getShouldShowContinueSetupButtonInitialValue());
206+
const [shouldShowContinueSetupButton, setShouldShowContinueSetupButton] = useState(() => getShouldShowContinueSetupButtonInitialValue());
207207

208208
const handleNextNonUSDBankAccountStep = () => {
209209
switch (nonUSDBankAccountStep) {

src/pages/home/report/ReportActionsList.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ function ReportActionsList({
162162
const reportScrollManager = useReportScrollManager();
163163
const userActiveSince = useRef<string>(DateUtils.getDBTime());
164164
const lastMessageTime = useRef<string | null>(null);
165-
const [isVisible, setIsVisible] = useState(Visibility.isVisible());
165+
const [isVisible, setIsVisible] = useState(Visibility.isVisible);
166166
const isFocused = useIsFocused();
167167

168168
const [reportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report?.reportID ?? -1}`);

src/pages/iou/request/step/IOURequestStepAttendees.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function IOURequestStepAttendees({
4040
}: IOURequestStepAttendeesProps) {
4141
const isEditing = action === CONST.IOU.ACTION.EDIT;
4242
const [transaction] = useOnyx(`${isEditing ? ONYXKEYS.COLLECTION.TRANSACTION : ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID || -1}`);
43-
const [attendees, setAttendees] = useState<Attendee[]>(TransactionUtils.getAttendees(transaction));
43+
const [attendees, setAttendees] = useState<Attendee[]>(() => TransactionUtils.getAttendees(transaction));
4444
const previousAttendees = usePrevious(attendees);
4545
const {translate} = useLocalize();
4646
const [violations] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`);

src/pages/settings/Profile/CustomStatus/StatusClearAfterPage.tsx

+5-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, {useCallback, useEffect, useMemo, useState} from 'react';
22
import {View} from 'react-native';
3-
import {withOnyx} from 'react-native-onyx';
4-
import type {OnyxEntry} from 'react-native-onyx';
3+
import {useOnyx} from 'react-native-onyx';
54
import type {ValueOf} from 'type-fest';
65
import FormProvider from '@components/Form/FormProvider';
76
import HeaderWithBackButton from '@components/HeaderWithBackButton';
@@ -19,7 +18,6 @@ import * as ValidationUtils from '@libs/ValidationUtils';
1918
import CONST from '@src/CONST';
2019
import ONYXKEYS from '@src/ONYXKEYS';
2120
import ROUTES from '@src/ROUTES';
22-
import type * as OnyxTypes from '@src/types/onyx';
2321

2422
type CustomStatusTypes = ValueOf<typeof CONST.CUSTOM_STATUS_TYPES>;
2523

@@ -30,13 +28,6 @@ type StatusType = {
3028
isSelected: boolean;
3129
};
3230

33-
type StatusClearAfterPageOnyxProps = {
34-
/** User's custom status */
35-
customStatus: OnyxEntry<OnyxTypes.CustomStatusDraft>;
36-
};
37-
38-
type StatusClearAfterPageProps = StatusClearAfterPageOnyxProps;
39-
4031
/**
4132
* @param data - either a value from CONST.CUSTOM_STATUS_TYPES or a dateTime string in the format YYYY-MM-DD HH:mm
4233
*/
@@ -80,14 +71,15 @@ const useValidateCustomDate = (data: string) => {
8071
return {customDateError, customTimeError, validateCustomDate};
8172
};
8273

83-
function StatusClearAfterPage({customStatus}: StatusClearAfterPageProps) {
74+
function StatusClearAfterPage() {
8475
const styles = useThemeStyles();
8576
const {translate} = useLocalize();
8677
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
8778
const clearAfter = currentUserPersonalDetails.status?.clearAfter ?? '';
79+
const [customStatus] = useOnyx(ONYXKEYS.CUSTOM_STATUS_DRAFT);
8880

8981
const draftClearAfter = customStatus?.clearAfter ?? '';
90-
const [draftPeriod, setDraftPeriod] = useState(getSelectedStatusType(draftClearAfter || clearAfter));
82+
const [draftPeriod, setDraftPeriod] = useState(() => getSelectedStatusType(draftClearAfter || clearAfter));
9183
const statusType = useMemo<StatusType[]>(
9284
() =>
9385
Object.entries(CONST.CUSTOM_STATUS_TYPES).map(([key, value]) => ({
@@ -222,8 +214,4 @@ function StatusClearAfterPage({customStatus}: StatusClearAfterPageProps) {
222214

223215
StatusClearAfterPage.displayName = 'StatusClearAfterPage';
224216

225-
export default withOnyx<StatusClearAfterPageProps, StatusClearAfterPageOnyxProps>({
226-
customStatus: {
227-
key: ONYXKEYS.CUSTOM_STATUS_DRAFT,
228-
},
229-
})(StatusClearAfterPage);
217+
export default StatusClearAfterPage;

src/pages/settings/Subscription/CardSection/CardSection.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function CardSection() {
6565
Navigation.navigate(ROUTES.SEARCH_CENTRAL_PANE.getRoute({query}));
6666
}, []);
6767

68-
const [billingStatus, setBillingStatus] = useState<BillingStatusResult | undefined>(CardSectionUtils.getBillingStatus(translate, defaultCard?.accountData ?? {}));
68+
const [billingStatus, setBillingStatus] = useState<BillingStatusResult | undefined>(() => CardSectionUtils.getBillingStatus(translate, defaultCard?.accountData ?? {}));
6969

7070
const nextPaymentDate = !isEmptyObject(privateSubscription) ? CardSectionUtils.getNextBillingDate() : undefined;
7171

src/pages/workspace/companyCards/assignCard/TransactionStartDateStep.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function TransactionStartDateStep() {
2727

2828
const [dateOptionSelected, setDateOptionSelected] = useState(data?.dateOption ?? CONST.COMPANY_CARD.TRANSACTION_START_DATE_OPTIONS.FROM_BEGINNING);
2929
const [isModalOpened, setIsModalOpened] = useState(false);
30-
const [startDate, setStartDate] = useState(format(new Date(), CONST.DATE.FNS_FORMAT_STRING));
30+
const [startDate, setStartDate] = useState(() => format(new Date(), CONST.DATE.FNS_FORMAT_STRING));
3131

3232
const handleBackButtonPress = () => {
3333
if (isEditing) {

0 commit comments

Comments
 (0)