如何在 react-native 中使用 datalist

问题描述

如何在 react-native 中使用 datalist。我想在带有 fetch api 的 react native 中使用 datalist。 我使用 laravel 作为后端,所以我想在 react native datalist

获取数据

解决方法

在 react-native 中使用 Flatlist 显示动态数据,如

import React,{ useEffect,useState } from 'react';
import { ActivityIndicator,FlatList,Text,View } from 'react-native';

export default App = () => {
  const [isLoading,setLoading] = useState(true);
  const [data,setData] = useState([]);

  useEffect(() => {
    fetch('https://reactnative.dev/movies.json')
      .then((response) => response.json())
      .then((json) => setData(json.movies))
      .catch((error) => console.error(error))
      .finally(() => setLoading(false));
  },[]);

  return (
    <View style={{ flex: 1,padding: 24 }}>
      {isLoading ? <ActivityIndicator/> : (
        <FlatList
          data={data}
          keyExtractor={({ id },index) => id}
          renderItem={({ item }) => (
            <Text>{item.title},{item.releaseYear}</Text>
          )}
        />
      )}
    </View>
  );
};
,

使用react-native-autocomplete-input

$ npm install --save react-native-autocomplete-input

render() {
  const { query } = this.state;
  const data = filterData(query);
  return (
    <Autocomplete
      data={data}
      value={query}
      onChangeText={(text) => this.setState({ query: text })}
      flatListProps={{
        keyExtractor: (_,idx) => idx,renderItem: ({ item }) => <Text>{item}</Text>,}}
    />
  );
}

react-native-autocomplete-input

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...