简体   繁体   English

搜索react native cloud firestore后如何恢复数据

[英]How to restore the data after search react native cloud firestore

So i have a problem that after done searching once name, when start type to search again.所以我有一个问题,在完成搜索一次名称后,当开始键入再次搜索时。

the data won't show again but after goBack() and go to the screen again data show, but only can search once more goBack() 和 go 到屏幕后数据不会再次显示,但只能再次搜索

Can anyone help谁能帮忙

Thank You谢谢你

const nameList = () => {
  const [daftar, setDaftar] = useState<any>([]);
  const [search, setSearch] = useState<any>('');

  const searchName = (input) => {
    let data = daftar;
    let searchData = data.filter((item) => {
      return (
        item?.name.toLowerCase().includes(input.toLowerCase()) ||
        item?.address?.toLowerCase().includes(input?.toLowerCase())
      );
    });
    setDaftar(searchData);
  };
     

  return (
    <Div flex={1} bg="#fff">
      <Div py={heightPercentageToDP(1)} px={heightPercentageToDP(2)}>
        <Input
          focusBorderColor={PRIMARY_COLOR}
          placeholder="Search..."
          onChangeText={(val) => searchName(val)}
          // value={search}
        />
      </Div>
    </Div>
  );
};

You should keep the filtered data in a different state.您应该将过滤后的数据保存在不同的 state 中。

Set the same data to both the deftar and the filteredList when the page is first opened.首次打开页面时,为deftarfilteredList设置相同的数据。 when you search you use the deftar datas and the result is set to filteredList当您搜索时,您使用deftar数据并将结果设置为filteredList

In the page use filteredList在页面中使用filteredList

const nameList = () => {
  const [daftar, setDaftar] = useState<any>([]);
  const [search, setSearch] = useState<any>('');
  const [filteredList, setFilterList] = useState<any>([]);

  const searchName = (input) => {
    let data = daftar;
    let searchData = data.filter((item) => {
      return (
        item?.name.toLowerCase().includes(input.toLowerCase()) ||
        item?.address?.toLowerCase().includes(input?.toLowerCase())
      );
    });
    setFilterList(searchData);
  };
     

  return (
    <Div flex={1} bg="#fff">
      <Div py={heightPercentageToDP(1)} px={heightPercentageToDP(2)}>
        <Input
          focusBorderColor={PRIMARY_COLOR}
          placeholder="Search..."
          onChangeText={(val) => searchName(val)}
          // value={search}
        />
      </Div>
    </Div>
  );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改谷歌帐户后反应本机 firestore 不上传数据 - React native firestore not uploading data after changing google account 使用 React Native 在平面列表中 Firestore 数据 - Firestore data in a flatlist with react native 如何在 React 本机应用程序中过滤 Firebase Firestore Array 数据? - How to filter Firebase Firestore Array data in React native app? 如何使用 react-native 在 Firestore 中的嵌套数组上添加数据 - How to add data on nested array in Firestore with react-native 如何在反应中从firebase云Firestore获取数据 - How to get data from firebase cloud firestore in react React Cloud Firestore 无法正确获取数据 - React Cloud Firestore Not Fetching Data Properly React Native 如何从 Firebase Firestore 获取数组数据? - React Native how to fetch Array data from Firebase Firestore? 在 React Native Cloud Firestore 中出现错误“@firebase/firestore: Firestore (9.15.0): Connection WebChannel transport errored” - Getting the error " @firebase/firestore: Firestore (9.15.0): Connection WebChannel transport errored" in React Native Cloud Firestore 在 React 中从 firebase Cloud Firestore 获取数据 - get data from firebase cloud firestore in react 无法到达 Cloud Firestore 后端 - React native Firebase v9 - Could not reach Cloud Firestore backend - React native Firebase v9
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM