如何使用搜索文本输入在屏幕上显示项目(expo, react-native)

问题描述 投票:0回答:1

我正在使用react native和expo。我在屏幕(iOS)模拟器上有一些JSON数据,这些数据是从API中获取的。在顶部,我有一个搜索栏,用户可以搜索屏幕上显示的数据。

例如,如果数据是

A

B

公司

A是公司标志,而 a company 是一个符号名。所以当用户输入A时,应该显示 A 公司,如果用户类型 Z 它应该显示 Z 公司。

我的代码。

import React, { useState, useEffect } from "react";
import {
  StyleSheet,
  View,
  TouchableWithoutFeedback,
  Keyboard,
  FlatList,
  TextInput,
  Button,
  Text,
} from "react-native";
import { useStocksContext } from "../contexts/StocksContext";
import { scaleSize } from "../constants/Layout";
import { Ionicons } from "@expo/vector-icons";

import { ListItem } from "react-native";

export default function SearchScreen({ navigation }) {
  const { ServerURL, addToWatchlist } = useStocksContext();

  const [state, setState] = useState({
    /*  initial state here */
    myListData: [],
    search: "",
  });

  useEffect(() => {
    renderWithData();
    // FixMe: fetch symbol names from the servner and save in local SearchScreen state
  }, []);

  updateSearch = (event) => {
    setState({ search: event.target.value });
  };

  renderWithData = () => {
    return fetch("http://131.181.190.87:3001/all")
      .then((res) => res.json())
      .then((json) => {
        setState({
          isLoaded: true,
          myListData: json,
        });
        setTimeout(() => {
          console.log(state.myListData);
        }, 10000);
      });
  };
  let filteredItems = state.myListData.filter((item) => {
    return (
      item.symbol.toUpperCase().indexOf(state.search.toUpperCase()) !== -1 ||
      item.name.indexOf(state.search) !== -1
    );
  });

  let movies = state.myListData.filteredItems.map((val) => {
    return (
      <View key={val.symbol} style={styles.text}>
        <Text style={styles.text}>{val.symbol}</Text>
        <Text style={styles.text}>{val.name}</Text>
      </View>
    );
  });

  return (
    <TouchableWithoutFeedback onPress={Keyboard.dismiss}>
      <View style={styles.container}>
        <TextInput
          style={styles.textinput}
          placeholder="Search here"
          placeholderTextColor="white"
          value={state.search}
          onChange={updateSearch.bind()}
        />

        <Text>csdn</Text>
        <View style={styles.text}>{movies}</View>
      </View>
    </TouchableWithoutFeedback>
  );
}

const styles = StyleSheet.create({
  textinput: {
    color: "white",
    height: "20",
    fontSize: 18,
  },
  text: {
    color: "white",
    backgroundColor: "black",
  },
  flatstuff: {
    color: "white",
  },

  // use scaleSize(x) to adjust sizes for small/large screens
});

我不知道我做错了什么,但是如果我在textinput上输入一些东西,它就不显示任何东西(更像是搜索的东西不工作),我的数据仍然显示在屏幕上,只是我不能用textinput搜索它。谁能帮帮我?

编辑:json数据

对象 {

"name": "Chesapeake Energy",
"symbol": "CHK",

}, 对象 {

"name": "C. H. Robinson Worldwide",
"symbol": "CHRW",

},

reactjs react-native search expo textinput
1个回答
1
投票

你应该在React-Native中使用像下面这样的textinput。

 <TextInput
      style={styles.textinput}
      placeholder="Search here"
      placeholderTextColor="white"
      value={state.search}
      onChangeText={text=>updateSearch(text)}
    />

你应该使用onChangeText,并且更新搜索应该像下面这样改变

updateSearch = (text) => {
    setState({ search: text });
  };

更新

这就是你的完整组件的样子,你可以试一试。

function SearchScreen({ navigation }) {
  const { ServerURL, addToWatchlist } = useStocksContext();

  const [state, setState] = useState({
    /*  initial state here */
    myListData: [],
  });
  const [search, setSearch] = useState('');

  useEffect(() => {
    renderWithData();
    // FixMe: fetch symbol names from the servner and save in local SearchScreen state
  }, []);

  const updateSearch = (text) => {
    setSearch(text);
  };

  renderWithData = () => {
    return fetch('http://131.181.190.87:3001/all')
      .then((res) => res.json())
      .then((json) => {
        setState({
          isLoaded: true,
          myListData: json,
        });
        setTimeout(() => {
          console.log(state.myListData);
        }, 10000);
      });
  };

  let filteredItems = state.myListData.filter((item) => {
    return (
      item.symbol.toUpperCase().indexOf(search.toUpperCase()) !== -1 ||
      item.name.indexOf(search) !== -1
    );
  });

  let movies = filteredItems.map((val) => {
    return (
      <View key={val.symbol} style={styles.text}>
        <Text style={styles.text}>{val.symbol}</Text>
        <Text style={styles.text}>{val.name}</Text>
      </View>
    );
  });

  return (
    <TouchableWithoutFeedback onPress={Keyboard.dismiss}>
      <View style={styles.container}>
        <TextInput
          style={styles.textinput}
          placeholder="Search here"
          placeholderTextColor="white"
          value={search}
          onChangeText={(text) => updateSearch(text)}
        />

        <Text>csdn</Text>
        <View style={styles.text}>{movies}</View>
      </View>
    </TouchableWithoutFeedback>
  );
}
© www.soinside.com 2019 - 2024. All rights reserved.