我正在做一个项目,我正在使用谷歌自动完成.
const SuggestionRow = ({ item }) => (
<View style={{}}>
<View style={{}}></View>
<Text style={{}}>{item.description}</Text>
</View>
);
const SCREEN_WIDTH = Dimensions.get("window").width;
export default function MapSearch({ setLocation }) {
let predefined = [];
const placesRef = useRef();
return (
<View style={{ width: SCREEN_WIDTH, paddingHorizontal: 5 }}>
<GooglePlacesAutocomplete
placeholder="Search"
onPress={(data, details) => {
setLocation(details);
}}
minLength={4}
fetchDetails
styles={{
textInput: styles.textInput,
}}
query={{
key: "",
language: "en",
}}
predefinedPlaces={predefined}
suppressDefaultStyles
ref={placesRef}
renderRow={(item) => <SuggestionRow item={item} />}
/>
</View>
);
}
我需要能够获得用户在他/她打字时键入的内容.我一直在try 使用useRef()来实现这一点,但是当我在屏幕上输入时,它没有更新.我曾try 使用useEffect执行此操作:
const [inputValue, setInputValue] = useState()
useEffect(() => {
setInputValue(placesRef.current?.getAddressText())
})
console.log('inputValue',inputValue)
但我在我的控制台上没有得到任何值或更新.我还try 使用useCallback():
const placesRef = useCallback(node => {
if (node?.current?.length > 0) {
setInputValue(node.current?.getAddressText();
}
}, []);
但这对我来说也不管用.我真的很感激任何关于如何做到这一点的帮助或建议.谢谢!