VirtualizedLists 不应该嵌套在具有相同方向的普通 ScrollViews 中 - 使用另一个 VirtualizedList-backed 容器代替

Arc*_*rma 7 react-native

我使用react-native-google-places-autocomplete包进行 google place 自动完成文本输入。但是在这里我每次点击地址后都会收到警告。

警告::

VirtualizedLists 不应该嵌套在具有相同方向的普通 ScrollViews 中 - 使用另一个 VirtualizedList-backed 容器代替。

我应该使用的另一个 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 };
Run Code Online (Sandbox Code Playgroud)

并在另一个组件中调用它,如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

当我点击自动完成时它会出现。

请建议我摆脱这个问题的解决方案,或者建议我一些其他包,它比这更好。

此警告来自此包的主文件。

小智 6

我在使用本机('来自 react-native')ScrollView 和 ListItem(来自 react-native-elements)时遇到了同样的问题。解决了它用视图组件包装的问题:

<View>
    <ScrollView>
        <View> {
            data.map((l, i) => (
                <ListItem
                />
            ))
        }
    </View>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)


Gol*_*Jer 4

这并不能解决问题,但如果您认为不存在真正的问题,它会消除警告。

将其添加到文件的顶部。

import { YellowBox } from 'react-native';

YellowBox.ignoreWarnings(['VirtualizedLists should never be nested']);
Run Code Online (Sandbox Code Playgroud)