-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
measureLayout doesn't work properly for ScrollView container #1957
Comments
It looks like this is because a
I don't know what this means. |
@necolas As you can see in the React Native docs the
The new variant of this method seems to fail in |
Didn't realise this had changed in 0.64. The plan is to eventually deprecate all the instance methods on RN refs, so I'm not sure why we've changing that API |
The Expo SDK v40 is based on RN |
Turns out the RN docs are confusing. You pass The issue is the scrollview ref is not a host component but a component instance. If you wrap the scrollview ref in a |
Feel free to update the docs, since you seems to have better understanding what's actually happening in there and additionally you have some internal knowledge too.
Hmm, but |
I'm not saying it should be required. I'm saying the reason you gave for this issue is incorrect and it is what I've already explained twice. |
I believe you can call |
This should be fixed in 0.15.5 |
@necolas This should be re-opened. I found a situation where the measurement is incorrect.
Here's a reproduction: https://codesandbox.io/s/lucid-sea-rq0ny?file=/src/App.js:0-1339 Code hereimport React from "react";
import { ScrollView, StyleSheet, Text, View } from "react-native";
const data = new Array(50).fill("").map((_, i) => i);
const HEIGHT = 100;
function App() {
const viewRefs = React.useRef({});
const scrollRef = React.useRef();
const scrollTo = (index) => () => {
viewRefs.current[index].measureLayout(scrollRef.current, (x, y) => {
console.log("[scroll-to]", { y, expected: index * HEIGHT });
scrollRef.current.scrollTo({
y,
animated: true
});
});
};
return (
<View style={styles.app}>
<ScrollView ref={scrollRef}>
{data.map((item, i) => (
<View
ref={(ref) => {
viewRefs.current[i] = ref;
}}
style={[styles.item, i % 2 ? styles.oddItem : styles.evenItem]}
key={item}
>
<Text onPress={scrollTo(i)} style={styles.itemText}>
{item}
</Text>
</View>
))}
</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
app: {
flex: 1
},
item: {
justifyContent: "center",
alignItems: "center",
height: HEIGHT
},
oddItem: {
backgroundColor: "#7928CA"
},
evenItem: {
backgroundColor: "black"
},
itemText: {
color: "white"
}
});
export default App;
It seems that By comparison, it works properly on iOS. Here is the same code on Expo Snack: https://snack.expo.dev/@beatgig/dedc14 (don't use this to test RNW, since they use 0.13 on Snack.) The problemThe issue is, For instance, if the scroll position is currently 100, and you measure they layout for an item that is at To summarize: Let me know if you need help testing a fix. Thanks! |
That's a different issue. Please open a new issue |
Make sure the 'ref' for a ScrollView is a host node with React Native's proprietary and legacy instance methods attached. Fix necolas#1957
The problem
I want to scroll to an element which is inside of a
ScrollView
. In order to get the element's scroll position, I'm trying to usemeasureLayout
.However, if I pass a
ScrollView
as the "container" inmeasureLayout
, the callback never fires.How to reproduce
Simplified test case: https://snack.expo.io/@nandorojo/measurelayout-example
Steps to reproduce:
ScrollView
, pass it aref: scrollRef
ref: textRef
textRef.current.measureLayout(scrollRef.current, callback)
Expected behavior
The third argument in
measureLayout
should fire the callback. However, nothing happens.If I change the "relative to" ref from
scrollRef
to aView
's ref, everything works as expected.When I run the code on React Native (iOS, Android) it works fine. You'll see this on the Expo Snack URL.
Environment (include versions). Did this work in previous versions?
^0.14.10
(Snack uses an older one, 0.13.x)16.13.1
I can't get it to work on older versions.
Additional context
I posted about this on the #web channel on the React Native discord.
@Simek reproduced the issue, and mentioned this:
The text was updated successfully, but these errors were encountered: