如何在功能性反应本机组件中找到组件位置
How do I find the component position in a functional react-native component
我正在尝试在呈现后动态查找我的组件的位置。我正在尝试使用 useRef 和 getBoundingClientRect()(请参见下面的代码)。
我得到的回复是这样的。
myRef.current.getBoundingClientRect is not a function. (In 'myRef.current.getBoundingClientRect()', 'myRef.current.getBoundingClientRect' is undefined).
知道我做错了什么吗?
import React, { useState, useRef } from "react";
import { View, Button, TextInput } from "react-native";
const MyComponent = () => {
const [value, onChangeText] = useState("Useless Placeholder");
const myRef = useRef();
const showRefPosition = () => {
console.log("button clicked, set focus and log position");
// this works and shows that i am using the ref correctly
myRef.current.focus();
// however, this does not work and throws the eror
let componentPosition = myRef.current.getBoundingClientRect();
console.log(`Component Position ${componentPosition}`);
};
return (
<View>
<TextInput
style={{ height: 40, borderColor: "gray", borderWidth: 1 }}
onChangeText={text => onChangeText(text)}
value={value}
ref={myRef}
/>
<Button title="Click Me" onPress={() => showRefPosition()} />
</View>
);
};
export default MyComponent;
你可以在react-native中尝试measure方法。
import React, { useState, useRef } from "react";
import { View, Button, TextInput } from "react-native";
const MyComponent = () => {
const [value, onChangeText] = useState("Useless Placeholder");
const myRef = useRef();
const showRefPosition = () => {
console.log("button clicked, set focus and log position");
// this works and shows that i am using the ref correctly
this.ref.measure( (width, height) => {
console.log('Component width is: ' + width)
console.log('Component height is: ' + height)
})
};
return (
<View>
<TextInput
style={{ height: 40, borderColor: "gray", borderWidth: 1 }}
onChangeText={text => onChangeText(text)}
value={value}
ref={(ref) => { this.ref = ref; }}
/>
<Button title="Click Me" onPress={() => showRefPosition()} />
</View>
);
};
export default MyComponent;
我正在尝试在呈现后动态查找我的组件的位置。我正在尝试使用 useRef 和 getBoundingClientRect()(请参见下面的代码)。
我得到的回复是这样的。
myRef.current.getBoundingClientRect is not a function. (In 'myRef.current.getBoundingClientRect()', 'myRef.current.getBoundingClientRect' is undefined).
知道我做错了什么吗?
import React, { useState, useRef } from "react";
import { View, Button, TextInput } from "react-native";
const MyComponent = () => {
const [value, onChangeText] = useState("Useless Placeholder");
const myRef = useRef();
const showRefPosition = () => {
console.log("button clicked, set focus and log position");
// this works and shows that i am using the ref correctly
myRef.current.focus();
// however, this does not work and throws the eror
let componentPosition = myRef.current.getBoundingClientRect();
console.log(`Component Position ${componentPosition}`);
};
return (
<View>
<TextInput
style={{ height: 40, borderColor: "gray", borderWidth: 1 }}
onChangeText={text => onChangeText(text)}
value={value}
ref={myRef}
/>
<Button title="Click Me" onPress={() => showRefPosition()} />
</View>
);
};
export default MyComponent;
你可以在react-native中尝试measure方法。
import React, { useState, useRef } from "react";
import { View, Button, TextInput } from "react-native";
const MyComponent = () => {
const [value, onChangeText] = useState("Useless Placeholder");
const myRef = useRef();
const showRefPosition = () => {
console.log("button clicked, set focus and log position");
// this works and shows that i am using the ref correctly
this.ref.measure( (width, height) => {
console.log('Component width is: ' + width)
console.log('Component height is: ' + height)
})
};
return (
<View>
<TextInput
style={{ height: 40, borderColor: "gray", borderWidth: 1 }}
onChangeText={text => onChangeText(text)}
value={value}
ref={(ref) => { this.ref = ref; }}
/>
<Button title="Click Me" onPress={() => showRefPosition()} />
</View>
);
};
export default MyComponent;