【发布时间】:2020-03-24 16:58:03
【问题描述】:
我使用react-native-google-places-autocomplete 包进行谷歌位置自动完成文本输入。但是在这里我每次点击地址后都会收到警告。
警告::
VirtualizedLists 永远不应该嵌套在普通的 ScrollViews 中 相同的方向 - 使用另一个 VirtualizedList 支持的容器 而是。
我应该使用的其他支持 VirtualizedList 的容器是什么,为什么现在建议不要那样使用?
我为此创建了一个自定义组件,下面我在 GooglePlacesInput 组件的渲染中添加代码::
class GooglePlacesInput extends PureComponent {
//..... other function
// render function
render() {
const { placeholder, minSearchLen, address, name, enablePoweredByContainer, currentLocation, onChangeText } = this.props;
return (
<GooglePlacesAutocomplete
placeholder={placeholder}
name={name}
minLength={minSearchLen}
autoFocus={false}
returnKeyType={'search'} // Can be left out for default return key https://facebook.github.io/react-native/docs/textinput.html#returnkeytype
keyboardAppearance={'light'} // Can be left out for default keyboardAppearance https://facebook.github.io/react-native/docs/textinput.html#keyboardappearance
listViewDisplayed="false" // true/false/undefined
fetchDetails={true}
getDefaultValue={() => address}
renderDescription={row => row.description} // custom description render
onPress={(data, details = null) => this.onPressAddress(data, details)}
onNotFound={() => { }}
query={{ key: GOOGLE_API_KEY }}
currentLocation={currentLocation} // Will add a 'Current location' button at the top of the predefined places list
currentLocationLabel="Current location"
nearbyPlacesAPI="GooglePlacesSearch"
textInputProps={{
onChangeText: onChangeText,
}}
enablePoweredByContainer={enablePoweredByContainer}
isRowScrollable={false}
styles={{
textInputContainer: {
backgroundColor: 'rgba(0,0,0,0)',
borderTopWidth: 0,
borderBottomWidth: 0,
width: '100%',
marginBottom: 20,
},
textInput: {
paddingHorizontal: 18,
height: 40,
color: Colors.grey,
fontSize: 16,
},
predefinedPlacesDescription: {
color: '#1faadb',
},
}}
/>
);
}
}
// default props values
GooglePlacesInput.defaultProps = {
name: 'address',
placeholder: 'Enter Address',
minSearchLen: 2,
address: '',
enablePoweredByContainer: true,
currentLocation: false,
onChangeText: '',
};
export { GooglePlacesInput };
然后像这样在另一个组件中调用它::
<Content
contentContainerStyle={[flexGrow1, p10]}
>
<View style={[alignItemsCenter, pt20, pb30]}>
<ImageView source={Images.mapIcon} style={{ height: Base.getPixel('4.5rem'), width: Base.getPixel('4.5rem') }} />
</View>
<Formik
initialValues={this.initialValues}
onSubmit={(data) => this.onSubmitForm(data)}
ref={el => (this.form = el)}
validationSchema={validationSchema}
isSubmitting={true}
render={({ handleSubmit, values, setValues, setFieldValue, errors, touched, ...props }) => {
return (
<Form>
<GooglePlacesInput
onPress={(data, detail) => this.onPressOfAddress(data, detail)}
address={values.address}
name="address"
onChangeText={(value) => setFieldValue('address', value)}
/>
<Button onPress={() => popScreen(this.props.componentId)} block large variant="danger">CANCEL</Button>
</Form>
);
}}
/>
</Content>
当我点击自动完成时会出现。
请建议我解决此问题的解决方案,或者建议我使用其他包,它比这更好。
此警告来自此软件包的主文件。
【问题讨论】:
-
你是在
<Content>还是<ScrollView>里面使用它? -
请检查这个包主文件,它在
github.com/FaridSafi/react-native-google-places-autocomplete/… 中包装了数据 -
如果我在 ScrollView 中添加其他内容,我的功能将无法正常工作。
-
我已经浏览了整个包/库,我要问的是你把
<GooglePlacesAutocomplete>包裹在<ScrollView>里面了吗? -
不,我将它包裹在
Content中,我在 Formik 中使用它
标签: react-native