【问题标题】:VirtualizedLists should never be nested inside plain ScrollViews with the same orientation - use another VirtualizedList-backed container insteadVirtualizedLists 不应该嵌套在具有相同方向的普通 ScrollViews 中 - 改用另一个 VirtualizedList-backed 容器
【发布时间】: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>

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

请建议我解决此问题的解决方案,或者建议我使用其他包,它比这更好。

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

【问题讨论】:

  • 你是在&lt;Content&gt;还是&lt;ScrollView&gt;里面使用它?
  • 请检查这个包主文件,它在 github.com/FaridSafi/react-native-google-places-autocomplete/… 中包装了数据
  • 如果我在 ScrollView 中添加其他内容,我的功能将无法正常工作。
  • 我已经浏览了整个包/库,我要问的是你把&lt;GooglePlacesAutocomplete&gt;包裹在&lt;ScrollView&gt;里面了吗?
  • 不,我将它包裹在 Content 中,我在 Formik 中使用它

标签: react-native


【解决方案1】:

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

<View>
    <ScrollView>
        <View> {
            data.map((l, i) => (
                <ListItem />
            ))}
        </View>
    </ScrollView>
</View>

【讨论】:

    【解决方案2】:

    UPDATE 忽略警告

    import { LogBox } from 'react-native';
    
    LogBox.ignoreLogs(['VirtualizedLists should never be nested inside plain ScrollViews with the same orientation - use another VirtualizedList-backed container instead.']);
    

    【讨论】:

      【解决方案3】:

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

      将它添加到文件的顶部。

      import { YellowBox } from 'react-native';
      
      YellowBox.ignoreWarnings(['VirtualizedLists should never be nested']);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-25
        • 1970-01-01
        • 1970-01-01
        • 2020-05-29
        • 2022-12-07
        • 2021-11-21
        相关资源
        最近更新 更多