Skip to content

Commit d4f1a4c

Browse files
authored
fix: access before initialization error (#1342)
## 📜 Description Fixed error. ```sh Metro error: Cannot access 'clampedScrollTarget' before initialization ReferenceError: Cannot access 'clampedScrollTarget' before initialization ``` ## 💡 Motivation and Context It seems like `export function` produces: <img width="774" height="673" alt="image" src="https://github.com/user-attachments/assets/6faf2e2d-de9c-4565-8adb-dba1c5cdc13d" /> While `export const` produces: <img width="1000" height="663" alt="image" src="https://github.com/user-attachments/assets/23cf8661-d418-4e55-8bf9-4314d591d732" /> ... <img width="1069" height="323" alt="image" src="https://github.com/user-attachments/assets/ac30e296-7afc-4ec7-891f-bbe774ae6457" /> So with first case we try to export function that hasn't been declared (though `function` should be hoisted?). Anyway across the package we often use `arrow functions` and it doesn't cause issues, so let's continue to use it. In this PR I re-worked `helpers` file and used arrow functions instead of regular functions. ## 📢 Changelog <!-- High level overview of important changes --> <!-- For example: fixed status bar manipulation; added new types declarations; --> <!-- If your changes don't affect one of platform/language below - then remove this platform/language --> ### JS - use `export const` instead of `export function`; - use arrow functions in `helpers`; ## 🤔 How Has This Been Tested? Tested in LegendList repo. ## 📸 Screenshots (if appropriate): |Before|After| |-------|-----| |<img width="774" height="673" alt="image" src="https://github.com/user-attachments/assets/6faf2e2d-de9c-4565-8adb-dba1c5cdc13d" />|<img width="1000" height="663" alt="image" src="https://github.com/user-attachments/assets/23cf8661-d418-4e55-8bf9-4314d591d732" />| ## 📝 Checklist - [x] CI successfully passed - [x] I added new mocks and corresponding unit-tests if library API was changed
1 parent 8393ee2 commit d4f1a4c

1 file changed

Lines changed: 15 additions & 15 deletions

File tree

  • src/components/KeyboardChatScrollView/useChatKeyboard

src/components/KeyboardChatScrollView/useChatKeyboard/helpers.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ const AT_END_THRESHOLD = 20;
1818
* getEffectiveHeight(150, 300, 50); // 125
1919
* ```
2020
*/
21-
export function getEffectiveHeight(
21+
export const getEffectiveHeight = (
2222
height: number,
2323
targetKeyboardHeight: number,
2424
offset: number,
25-
): number {
25+
): number => {
2626
"worklet";
2727

2828
if (offset === 0 || targetKeyboardHeight === 0) {
@@ -34,7 +34,7 @@ export function getEffectiveHeight(
3434
[0, targetKeyboardHeight],
3535
[0, Math.max(targetKeyboardHeight - offset, 0)],
3636
);
37-
}
37+
};
3838

3939
/**
4040
* Check whether the scroll view is at the end of its content.
@@ -54,20 +54,20 @@ export function getEffectiveHeight(
5454
* const atEndInverted = isScrollAtEnd(5, 800, 2000, true); // true (5 <= 20)
5555
* ```
5656
*/
57-
export function isScrollAtEnd(
57+
export const isScrollAtEnd = (
5858
scrollOffset: number,
5959
layoutHeight: number,
6060
contentHeight: number,
6161
inverted: boolean = false,
62-
): boolean {
62+
): boolean => {
6363
"worklet";
6464

6565
if (inverted) {
6666
return scrollOffset <= AT_END_THRESHOLD;
6767
}
6868

6969
return scrollOffset + layoutHeight >= contentHeight - AT_END_THRESHOLD;
70-
}
70+
};
7171

7272
/**
7373
* Decide whether content should be shifted based on the keyboard lift behavior.
@@ -81,10 +81,10 @@ export function isScrollAtEnd(
8181
* shouldShiftContent("whenAtEnd", false); // false
8282
* ```
8383
*/
84-
export function shouldShiftContent(
84+
export const shouldShiftContent = (
8585
behavior: KeyboardLiftBehavior,
8686
isAtEnd: boolean,
87-
): boolean {
87+
): boolean => {
8888
"worklet";
8989

9090
switch (behavior) {
@@ -97,7 +97,7 @@ export function shouldShiftContent(
9797
case "persistent":
9898
return true;
9999
}
100-
}
100+
};
101101

102102
/**
103103
* Compute the clamped scroll target for non-inverted lists.
@@ -112,18 +112,18 @@ export function shouldShiftContent(
112112
* clampedScrollTarget(100, 300, 1000, 800); // 400
113113
* ```
114114
*/
115-
export function clampedScrollTarget(
115+
export const clampedScrollTarget = (
116116
offsetBeforeScroll: number,
117117
keyboardHeight: number,
118118
contentHeight: number,
119119
layoutHeight: number,
120-
): number {
120+
): number => {
121121
"worklet";
122122

123123
const maxScroll = Math.max(contentHeight - layoutHeight + keyboardHeight, 0);
124124

125125
return Math.min(Math.max(offsetBeforeScroll + keyboardHeight, 0), maxScroll);
126-
}
126+
};
127127

128128
/**
129129
* Compute contentOffset.y for iOS lists.
@@ -140,14 +140,14 @@ export function clampedScrollTarget(
140140
* computeIOSContentOffset(100, 300, 1000, 800, false); // 400
141141
* ```
142142
*/
143-
export function computeIOSContentOffset(
143+
export const computeIOSContentOffset = (
144144
relativeScroll: number,
145145
keyboardHeight: number,
146146
contentHeight: number,
147147
layoutHeight: number,
148148
inverted: boolean,
149149
extraContentPadding: number = 0,
150-
): number {
150+
): number => {
151151
"worklet";
152152

153153
if (inverted) {
@@ -165,4 +165,4 @@ export function computeIOSContentOffset(
165165
);
166166

167167
return Math.min(Math.max(keyboardHeight + relativeScroll, 0), maxScroll);
168-
}
168+
};

0 commit comments

Comments
 (0)