I have some json data that I fetch from api and display on the iOS simulator based on user input.
There is a searchbar at the top of screen where user can type any symbol values in it and it should display that specific value on the screen.
My json data:
Object {
"name": "Chesapeake Energy",
"symbol": "CHK",
},
Object {
"name": "C. H. Robinson Worldwide",
"symbol": "CHRW",
},
Hence, if user type CHK it should display symbol and name as shown below:
CHK
Chesapeake Energy
My code:
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();
}, []);
renderWithData = () => {
return fetch("http://131.181.190.87:3001/all")
.then((res) => res.json())
.then((json) => {
setState({
isLoaded: true,
myListData: json,
});
setTimeout(() => {
console.log(json);
}, 1000);
});
};
let onChangeText = (event) => {
setState({ search: event }); //here it does not set the value of event(userinput) to search
};
return (
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<View style={styles.container}>
<TextInput
style={styles.textinput}
placeholder="Search here"
placeholderTextColor="white"
onChangeText={(search) => onChangeText(search)}
/>
</View>
</TouchableWithoutFeedback>
);
}
My question is how do I write my onChangeText() so I can display the symbol and name on screen based on user input.
let onChangeText = (event) => {
setState({ search: event }); //here it does not set the value of event(userinput) to search
};
Here I am trying to set the user value (event) to the search, but it does not set the value to it, but if I print event I can see whatever user types in my console. How do I display the symbol and name based on what user types? Is there a way to filter those?