Unable to Pass the data to API using react-native-element-dropdown - react-native

How to pass the data to API?
//Parent Componet - in updateval function, i m trying get the selected values from dropdown but i am not getting to know how to get that.
import ElementDropdown from '../Components/ElementDropdown';
class ScanScreen extends Component {
constructor(props) {
super(props);
}
updatedVal=(item,value)=>{
console.log("hgfgf",item,value);
}
render() {
return (
<ElementDropdown text={'Country'} label={['Germany','India']} updatedVal={this.updatedVal}/>
<ElementDropdown text={'Store'} label={['Dusseldolf','Bangalore']} updatedVal={this.updatedVal}/>
)
}
}
export default ScanScreen;
//Child component - I m trying to send selected values from dropdown list to parent in updatedval.
import React, { useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Dropdown } from 'react-native-element-dropdown';
import BPText from './BPText';
import ThemeColors from '../themes/bp-themes/variables/themeColors';
const ElementDropdown = (props) => {
const { text, label,updatedVal } = props;
const [value, setValue] = useState(null);
const [isFocus, setIsFocus] = useState(false);
const data = [
{ label: label[0], value: "id1" },
{ label: label[1], value: "id2" },
];
return (
<View style={styles.container}>
<View style={styles.textContainer}>
<BPText type="Light" style={{ color: ThemeColors.themeColorYellow }}>
{text}
</BPText>
</View>
<View style={styles.dropContainer}>
<Dropdown
style={[styles.dropdown, isFocus && { borderColor: 'blue' }]}
selectedTextStyle={styles.selectedTextStyle}
iconStyle={styles.iconStyle}
placeholderStyle={styles.placeholderStyle}
data={data}
maxHeight={300}
labelField="label"
valueField="value"
item
placeholder={label[0]}
value={value}
onFocus={() => setIsFocus(true)}
onBlur={() => setIsFocus(false)}
onChange={item => {
setValue(item.value);
setIsFocus(false);
//setIsDefalut(false);
updatedVal(item,label[0],label[1]);
}}
/>
</View>
</View>
);
};
export default ElementDropdown;
const styles = StyleSheet.create({
container: {
borderBottomWidth: 0.5,
borderBottomColor: 'white',
flexDirection: 'row',
marginLeft: 40,
marginRight: 40,
marginTop: 10,
justifyContent: 'center',
alignItems: 'center',
},
textContainer:{
flex:1
},
dropContainer: {
flex: 2,
},
dropdown: {},
icon: {
marginRight: 5,
},
selectedTextStyle: {
color: 'white',
},
iconStyle: {
width: 20,
height: 20,
tintColor: 'white'
},
placeholderStyle:{
color:'white'
}
});

Related

how to search item in listed element react native

I am stuck with a problem, when am trying to search elements from listed data it's not working, I seem lots of document ad I applied it, but still, its work for me, can anyone help me.
if you have any questions please free feel to ask any time.
home.js
This is Home.js file where I wrote my all code. and here I use some React native paper components.
import React, { useEffect, useState } from 'react'
import { Text, View, FlatList, StyleSheet, ScrollView, TouchableOpacity } from 'react-native';
import { Avatar } from 'react-native-elements';
import { Searchbar, Provider, Portal, TextInput, Button } from 'react-native-paper';
import { AntDesign, Entypo } from '#expo/vector-icons';
import Modal from "react-native-modal";
export default function Home() {
const [searchquery, setSearchquery] = React.useState();
const [isModalVisible, setModalVisible] = useState(false);
const [name, setName] = React.useState('');
const [number, setNumber] = React.useState('');
const toggleModal = () => {
setModalVisible(!isModalVisible);
};
const filterItem = (text) => {
users.filter((item) => {
return item.name.toLowerCase().inludes(text.toLowerCase())
})
setSearchquery(text)
}
const [users, setUser] = useState([
{
id: 1,
name: "Ashish Nirvikar",
number: 3289768909,
},
{
id: 2,
name: "Drew Macntyre",
number: 3345661276
},
{
id: 3,
name: "Jonh Cena",
number: 9087392878
},
{
id: 4,
name: "Rock Samoa",
number: 9723780928
},
{
id: 5,
name: "Boby Lashely",
number: 8769213678
},
{
id: 6,
name: "Seth Rollins",
number: 6890326741
},
])
return (
<View >
<Searchbar
placeholder="Search Contacts"
onChangeText={(text) => setSearchquery(text)}
value={searchquery}
style={{ marginTop: 30, marginHorizontal: 10 }}
/>
<ScrollView>
{
filterItem(users).map((item, index) => {
return (
<View key={index} style={styles.names}>
<Text style={{ color: 'black', fontSize: 20, fontWeight: 'bold' }}>Name : {item.name}</Text>
<Text style={{ color: 'black' }}>Mobile no : {item.number}</Text>
</View>
)
})
}
</ScrollView>
<Modal isVisible={isModalVisible} style={{ backgroundColor: 'white', height: 50, marginBottom: 200, marginTop: 100 }}>
<View style={{ flex: 2 }}>
<Text style={{ color: 'black', fontSize: 50, textAlign: 'center' }}>Add Contact</Text>
<TextInput
style={styles.input}
label="Enter Full name"
value={name}
onChangeText={text => setName(text)}
/>
<TextInput
style={styles.input1}
label="Enter Mobile Number"
value={number}
onChangeText={text => setNumber(text)}
/>
<Button title="Hide modal" onPress={toggleModal} style={{ color: 'black', backgroundColor: 'white', borderWidth: 1, borderColor: 'gray', marginHorizontal: 10, marginTop: 15 }}>Cancle</Button>
</View>
</Modal>
<AntDesign name="plus" size={34} color="black" style={styles.plus} onPress={toggleModal} />
</View>
);
}
const styles = StyleSheet.create({
customText: {
padding: 10,
marginTop: 20,
textAlign: 'center',
backgroundColor: 'lightgray',
fontWeight: 'bold',
fontSize: 20
},
plus: {
fontSize: 50,
position: 'absolute',
top: 680,
right: 40,
backgroundColor: 'pink',
borderRadius: 15,
borderWidth: 0.5,
padding: 5,
},
names: {
padding: 15,
fontSize: 25,
fontWeight: 'bold',
backgroundColor: 'lightgray',
marginTop: 10,
borderRadius: 20,
color: 'black'
},
addcontactname: {
fontSize: 30,
textAlign: 'center',
marginTop: 10,
marginBottom: 30
},
input: {
marginHorizontal: 10,
marginBottom: 20,
marginTop: 20
},
input1: {
marginHorizontal: 10,
marginBottom: 10,
}
});
Your Searchbar should be a controlled input (call setSearchquery with the change value from the Searchbar).
Then you can use the value of searchquery to perform the filtering inline in your jsx.
Finally, use FlatList to render a list of items instead of a map inside a ScrollView.
In your example, there's no need for the list of users to be stored in state.
import React, { Component } from 'react';
import { Text, View, StyleSheet, ScrollView, FlatList } from 'react-native';
import { Searchbar } from 'react-native-paper';
const data = [...];
export default class Home extends Component {
constructor(props) {
super(props);
this.state = {
searchquery: '',
};
}
renderHeader = () => {
return (
<Searchbar
placeholder="Search Contacts"
onChangeText={(searchquery) =>
this.setState({
searchquery,
})
}
value={this.state.searchquery}
/>
);
};
renderItem = ({ item, index }) => {
return (
<View key={index} style={styles.names}>
<Text>
Name : {item.name}
</Text>
<Text>Mobile no : {item.number}</Text>
</View>
);
};
render() {
return (
<FlatList
data={data.filter((item) => {
if (!this.state.searchquery) return true
return item.name
.toUpperCase()
.includes(this.state.searchquery.toUpperCase());
})}
ListHeaderComponent={this.renderHeader}
renderItem={this.renderItem}
/>
);
}
}
Snack
you can try with this code if it will not work then call filterItem function with bracket and parameter also my code is working while the name is lowercase.
<Searchbar
placeholder="Search Contacts"
onChangeText={filterItem}
onClear={(users) => setUser('')}
value={searchquery}
style={{ marginTop: 30, marginHorizontal: 10 }}
/>
const filterItem = (text) => {
setSearchquery(text)
let searchText = text.toLowerCase()
let filterData = users
filterData = filterData.filter(function (id) {
return id.name.toLowerCase().includes(searchText)
})
setUser(filterData);
if (text === "") {
setUser(
[
{
id: 1,
name: "Ashish Nirvikar",
number: 3289768909,
},
{
id: 2,
name: "Drew Macntyre",
number: 3345661276
},
{
id: 3,
name: "Jonh Cena",
number: 9087392878
},
{
id: 4,
name: "Rock Samoa",
number: 9723780928
},
{
id: 5,
name: "Boby Lashely",
number: 8769213678
},
{
id: 6,
name: "Seth Rollins",
number: 6890326741
},
]
);
}
}
Try this
const filterItem = (searchItems) => {
searchItems.filter((item) => {
return item.name.toLowerCase().inludes(searchquery.toLowerCase())
})
}
<Searchbar
placeholder="Search Contacts"
onChangeText={(text) => setSearchquery(text)}
onClear={(text) => searchquery("")}
value={searchquery}
style={{ marginTop: 30, marginHorizontal: 10 }}
/>
<ScrollView>
{
filterItem(users).map((item, index) => {
return (
<View key={index} style={styles.names}>
<Text style={{ color: 'black', fontSize: 20, fontWeight: 'bold' }}>Name : {item.name}</Text>
<Text style={{ color: 'black' }}>Mobile no : {item.number}</Text>
</View>
)
})
}
</ScrollView>

how to get current flat list item data in reactnative using react-native-swipe-list-view

I'm not getting how to get the current item object of a flat list
I used react-native-swipeout and react-native-swipe-list-view and in both examples I stuck.
And on google I found very big answers. Which are not pointing only the particular issue which confused me a lot.
the below is the deleted function when I used react-native-swipeout plugin
const swipeSettings = {
left: [
{
text: 'Delete',
onPress: () => {
console.log('-----delete-----');
},
type: 'delete',
},
}
All I need is to get the current item object data like below inside onpress() when i tapped the delete button .
{
id: 1,
prodName : "abcdefg",
}
That's all, I came from native script background and in that framework I never faced such issue. Because the documentation was crystal clear. But here In react native everything seems to be complicated for me.
Kindly any one help me.
<FlatList
data={data}
keyExtractor={(item) => item.id}
renderItem={({item, index}) => (
<Swipeout {...swipeSettings}>
<View style={styles.listView}>
<Text style={styles.listViewText}>{item.prodName}</Text>
</View>
</Swipeout>
)}
/>
entire page
import React, {useState} from 'react';
import {View} from 'react-native-animatable';
import {
TextInput,
TouchableOpacity,
FlatList,
} from 'react-native-gesture-handler';
import Icon from 'react-native-vector-icons/FontAwesome';
import {StyleSheet, Pressable, Text, Button} from 'react-native';
import * as Animatable from 'react-native-animatable';
import Swipeout from 'react-native-swipeout';
import ButtonPressable from '../../components/ButtonPressable';
var sqlite_wrapper = require('./sqliteWrapper');
const DATA = [
{
prodName: 'Added data will look like this',
},
];
const swipeSettings = {
style: {
marginBottom: 10,
},
autoClose: false,
backgroundColor: 'transparent',
close: false,
disabled: false,
onClose: (sectionID, rowId, direction) => {
console.log('---onclose--');
},
onOpen: (sectionID, rowId, direction) => {
console.log('---onopen--');
},
right: [
{
backgroundColor: 'dodgerblue',
color: 'white',
text: 'Edit',
onPress: () => {
console.log('-----edit-----');
},
},
],
left: [
{
backgroundColor: 'red',
color: 'white',
text: 'Delete',
onPress: () => {
console.log('-----delete-----');
sqlite_wrapper.deleteById
},
type: 'delete',
// component : (<ButtonPressable text="delete" />)
},
],
// buttonWidth: 100,
};
const AddProductList = ({route, navigation}) => {
const {navData} = route.params;
const [prodName, setProdName] = useState('');
var [data, setData] = useState(DATA);
return (
<View style={styles.container}>
<Animatable.View
animation="bounceIn"
duration={1000}
style={styles.inputFieldView}>
<TextInput
style={styles.textInput}
onChangeText={(value) => setProdName(value)}
placeholder="Add the Product"
defaultValue={prodName}
/>
<TouchableOpacity
style={styles.addView}
onPress={() => {
sqlite_wrapper
.insert({prodName: prodName}, sqlite_wrapper.collection_product)
.then((result) => {
console.log('---result---');
console.log(result);
if (result.rowsAffected) {
fetchAllData();
}
});
function fetchAllData() {
sqlite_wrapper
.readAll(sqlite_wrapper.collection_product)
.then((resultData) => {
console.log('---resultData---');
console.log(resultData);
setData(resultData);
setProdName('');
});
}
// without sql this is how to update the state having a array
// const updatedArray = [...data];
// updatedArray.push({prodName: prodName});
// setData(updatedArray);
// setProdName('');
}}>
<Icon name="plus" size={16} style={styles.add} color="white" />
</TouchableOpacity>
</Animatable.View>
<Animatable.View
animation="bounceInLeft"
duration={1500}
style={{flex: 1}}>
<FlatList
data={data}
keyExtractor={(item) => item.id}
renderItem={({item, index}) => (
<Swipeout {...swipeSettings}>
<View style={styles.listView}>
<Text style={styles.listViewText}>{item.prodName}</Text>
</View>
</Swipeout>
)}
/>
</Animatable.View>
</View>
);
};
var styles = StyleSheet.create({
container: {
flex: 1,
},
inputFieldView: {
flexDirection: 'row',
alignItems: 'center',
alignSelf: 'stretch',
margin: 10,
},
textInput: {
flex: 1,
backgroundColor: '#b2ebf2',
borderTopLeftRadius: 7,
borderBottomLeftRadius: 7,
fontSize: 16,
},
addView: {
backgroundColor: '#0f4c75',
alignSelf: 'stretch',
alignItems: 'center',
justifyContent: 'center',
borderTopEndRadius: 7,
borderBottomEndRadius: 7,
padding: 9,
},
add: {
padding: 7,
},
listView: {
padding: 20,
backgroundColor: 'green',
margin: 0,
borderRadius: 0,
},
listViewText: {
color: 'white',
},
});
export default AddProductList;
So if I get you right you have one generic swipe setting that you want to adjust to each element in the list.
Try changing the renderItem of the Flatlist like this and let me know how it worked for you:
<FlatList
data={data}
keyExtractor={(item) => item.id}
renderItem={({ item, index }) => {
let right = [...swipeSettings.right]; <---- ADDED
let left = [...swipeSettings.left]; <---- ADDED
right[0].onPress = () => console.log('edit', item.prodName); <---- ADDED
left[0].onPress = () => console.log('delete', item.prodName); <---- ADDED
<Swipeout {...swipeSettings} {...{ right, left}} > <---- CHANGED
<View style={styles.listView}>
<Text style={styles.listViewText}>{item.prodName}</Text>
</View>
</Swipeout>
}}
/>
So what I have done here is sending the generic swipe settings to each instance of Swipeout but changed their "Left" and "Right" in order to overwrite their "onPress" function and adjust it to the rendered item.
I know it is hard to understand it like this and it's probably not the best explanation but I hope it will help you somehow.
EDIT
import React, { useState } from 'react';
import { View } from 'react-native-animatable';
import {
TextInput,
TouchableOpacity,
FlatList,
} from 'react-native-gesture-handler';
import Icon from 'react-native-vector-icons/FontAwesome';
import { StyleSheet, Pressable, Text, Button } from 'react-native';
import * as Animatable from 'react-native-animatable';
import Swipeout from 'react-native-swipeout';
import ButtonPressable from '../../components/ButtonPressable';
var sqlite_wrapper = require('./sqliteWrapper');
const DATA = [
{
prodName: 'Added data will look like this',
},
];
const AddProductList = ({ route, navigation }) => {
const { navData } = route.params;
const [prodName, setProdName] = useState('');
var [data, setData] = useState(DATA);
const renderItem = ({ item, index }) => {
const swipeSettings = {
style: {
marginBottom: 10,
},
autoClose: false,
backgroundColor: 'transparent',
close: false,
disabled: false,
onClose: (sectionID, rowId, direction) => {
console.log('---onclose--');
},
onOpen: (sectionID, rowId, direction) => {
console.log('---onopen--');
},
right: [
{
backgroundColor: 'dodgerblue',
color: 'white',
text: 'Edit',
onPress: () => console.log('-----edit-----', item.prodName)
},
],
left: [
{
backgroundColor: 'red',
color: 'white',
text: 'Delete',
onPress: () => {
console.log('-----delete-----', item.prodName);
sqlite_wrapper.deleteById
},
type: 'delete',
// component : (<ButtonPressable text="delete" />)
},
],
// buttonWidth: 100,
};
return (
<Swipeout {...swipeSettings}>
<View style={styles.listView}>
<Text style={styles.listViewText}>{item.prodName}</Text>
</View>
</Swipeout>
)
}
return (
<View style={styles.container}>
<Animatable.View
animation="bounceIn"
duration={1000}
style={styles.inputFieldView}>
<TextInput
style={styles.textInput}
onChangeText={(value) => setProdName(value)}
placeholder="Add the Product"
defaultValue={prodName}
/>
<TouchableOpacity
style={styles.addView}
onPress={() => {
sqlite_wrapper
.insert({ prodName: prodName }, sqlite_wrapper.collection_product)
.then((result) => {
console.log('---result---');
console.log(result);
if (result.rowsAffected) {
fetchAllData();
}
});
function fetchAllData() {
sqlite_wrapper
.readAll(sqlite_wrapper.collection_product)
.then((resultData) => {
console.log('---resultData---');
console.log(resultData);
setData(resultData);
setProdName('');
});
}
// without sql this is how to update the state having a array
// const updatedArray = [...data];
// updatedArray.push({prodName: prodName});
// setData(updatedArray);
// setProdName('');
}}>
<Icon name="plus" size={16} style={styles.add} color="white" />
</TouchableOpacity>
</Animatable.View>
<Animatable.View
animation="bounceInLeft"
duration={1500}
style={{ flex: 1 }}>
<FlatList
data={data}
keyExtractor={(item) => item.id}
renderItem={renderItem}
/>
</Animatable.View>
</View>
);
};
var styles = StyleSheet.create({
container: {
flex: 1,
},
inputFieldView: {
flexDirection: 'row',
alignItems: 'center',
alignSelf: 'stretch',
margin: 10,
},
textInput: {
flex: 1,
backgroundColor: '#b2ebf2',
borderTopLeftRadius: 7,
borderBottomLeftRadius: 7,
fontSize: 16,
},
addView: {
backgroundColor: '#0f4c75',
alignSelf: 'stretch',
alignItems: 'center',
justifyContent: 'center',
borderTopEndRadius: 7,
borderBottomEndRadius: 7,
padding: 9,
},
add: {
padding: 7,
},
listView: {
padding: 20,
backgroundColor: 'green',
margin: 0,
borderRadius: 0,
},
listViewText: {
color: 'white',
},
});
export default AddProductList;

Convert React Native functional components to class components

I'm new to React Native and after following some tutorials I hacked this together but now I want to load some gifs right when the app starts - not after the button click.
Did some research and it looks like it's not possible with functional components and I need to switch to class components to use lifecycle functions like:
componentWillMount(){
this.setState({data : inputObject});
}
All the examples I've read so far don't have functions in their components and I can't figure out what to do with them. So if it is possible to call a function when the app starts using this as is please let me know how, if not, how do I convert this code to class component style? Thanks!
import React, {useState} from 'react';
import {
Dimensions,
StyleSheet,
SafeAreaView,
View,
Image,
FlatList,
} from 'react-native';
import SearchInput from './SearchInput';
export default function App() {
const [allGifResults, setAllGifResults] = useState([]);
function addSearchResultsHandler(searchTerm) {
console.log(searchTerm);
setAllGifResults([]);
fetchResults(searchTerm);
}
function allGifResultsHandler(url) {
setAllGifResults(currentGifs => [...currentGifs, {id: url, value: url}]);
}
function fetchResults(searchTerm) {
fetch(
'http://api.giphy.com/v1/gifs/search?q=' +
searchTerm +
'&api_key=MKSpDwx7kTCbRp23VtVsP4d0EvfwIgSg&limit=50',
)
.then(response => response.json())
.then(responseJson => {
for (let item of responseJson.data) {
allGifResultsHandler(item.images.fixed_height.url);
console.log(item.images.fixed_height.url);
}
})
.catch(error => {
console.error(error);
});
}
return (
<SafeAreaView style={styles.container}>
<View style={styles.screen}>
<SearchInput onSearchButtonPressed={addSearchResultsHandler} />
</View>
<FlatList
keyExtractor={(item, index) => item.id}
data={allGifResults}
numColumns={2}
renderItem={itemData => (
<Image
source={itemData.item.value ? {uri: itemData.item.value} : null}
style={styles.images}
/>
)}
/>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
screen: {
margin: 10,
},
images: {
width: Dimensions.get('window').width / 2 - 20,
height: Dimensions.get('window').width / 2 - 20,
margin: 10,
},
});
import React, {useState} from 'react';
import {
View,
TextInput,
TouchableOpacity,
Text,
StyleSheet,
} from 'react-native';
function SearchInput(props) {
const [searchTerm, setSearchTerm] = useState('');
function inputHandler(enteredText) {
setSearchTerm(enteredText);
}
return (
<View style={styles.inputContainer}>
<TextInput
placeholder="Search Term"
style={styles.input}
onChangeText={inputHandler}
value={searchTerm}
/>
<TouchableOpacity
style={styles.searchButton}
onPress={props.onSearchButtonPressed.bind(this, searchTerm)}>
<Text style={styles.searchButtonText}>Search</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
inputContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'flex-start',
marginBottom: 20,
},
input: {
width: '70%',
borderColor: 'black',
borderWidth: 1,
fontSize: 16,
},
searchButton: {
height: 50,
width: 100,
backgroundColor: 'lightblue',
marginLeft: 10,
},
searchButtonText: {
height: 50,
fontSize: 18,
textAlign: 'center',
textAlignVertical: 'center',
},
});
export default SearchInput;
import React, {useState} from 'react';
import {
Dimensions,
StyleSheet,
SafeAreaView,
View,
Image,
FlatList,
} from 'react-native';
import SearchInput from './SearchInput';
const [allGifResults, setAllGifResults] = useState([]);
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
};
this.addSearchResultsHandler = this.addSearchResultsHandler.bind(this);
this.allGifResultsHandler = this.allGifResultsHandler.bind(this);
this.fetchResults = this.fetchResults.bind(this);
}
addSearchResultsHandler(searchTerm) {
console.log(searchTerm);
setAllGifResults([]);
fetchResults(searchTerm);
}
allGifResultsHandler(url) {
setAllGifResults(currentGifs => [...currentGifs, {id: url, value: url}]);
}
fetchResults(searchTerm) {
fetch(
'http://api.giphy.com/v1/gifs/search?q=' +
searchTerm +
'&api_key=MKSpDwx7kTCbRp23VtVsP4d0EvfwIgSg&limit=50',
)
.then(response => response.json())
.then(responseJson => {
for (let item of responseJson.data) {
allGifResultsHandler(item.images.fixed_height.url);
console.log(item.images.fixed_height.url);
}
})
.catch(error => {
console.error(error);
});
}
render(){
return (
<SafeAreaView style={styles.container}>
<View style={styles.screen}>
<SearchInput onSearchButtonPressed={(data)=> this.addSearchResultsHandler(data)} />
</View>
<FlatList
keyExtractor={(item, index) => item.id}
data={allGifResults}
numColumns={2}
renderItem={itemData => (
<Image
source={itemData.item.value ? {uri: itemData.item.value} : null}
style={styles.images}
/>
)}
/>
</SafeAreaView>
);
}
}
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
screen: {
margin: 10,
},
images: {
width: Dimensions.get('window').width / 2 - 20,
height: Dimensions.get('window').width / 2 - 20,
margin: 10,
},
});
import React, {useState} from 'react';
import {
View,
TextInput,
TouchableOpacity,
Text,
StyleSheet,
} from 'react-native';
const [searchTerm, setSearchTerm] = useState('');
class SearchInput extends React.Component {
constructor(props) {
super(props);
this.state = {
};
this.inputHandler = this.inputHandler.bind(this);
}
inputHandler(enteredText) {
setSearchTerm(enteredText);
}
render(){
return (
<View style={styles.inputContainer}>
<TextInput
placeholder="Search Term"
style={styles.input}
onChangeText={inputHandler}
value={searchTerm}
/>
<TouchableOpacity
style={styles.searchButton}
onPress={props.onSearchButtonPressed.bind(this, searchTerm)}>
<Text style={styles.searchButtonText}>Search</Text>
</TouchableOpacity>
</View>
);
}
}
export default SearchInput;
const styles = StyleSheet.create({
inputContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'flex-start',
marginBottom: 20,
},
input: {
width: '70%',
borderColor: 'black',
borderWidth: 1,
fontSize: 16,
},
searchButton: {
height: 50,
width: 100,
backgroundColor: 'lightblue',
marginLeft: 10,
},
searchButtonText: {
height: 50,
fontSize: 18,
textAlign: 'center',
textAlignVertical: 'center',
},
});
export default SearchInput;

react-native-autocomplete result list not touchable

I have this implementation of a screen
import React, { Component } from 'react';
import { StyleSheet } from 'react-native';
import { Icon, View, Text, ListItem } from 'native-base';
import { createMaterialTopTabNavigator } from 'react-navigation';
import { connect } from 'react-redux';
import Autocomplete from 'native-base-autocomplete';
import EventsResults from './EventsResults';
import VehiclesResults from './VehiclesResults';
import { performSearch } from '../actions/search';
import Colors from '../../../util/Colors';
import GlobalStyle from '../../../util/GlobalStyle';
const styles = StyleSheet.create({
autocompleteContainer: {
zIndex: 800
}
});
const mapStateToProps = ({ searchResults }) => ({
searchResults
});
const SearchTabNavigatorStack = createMaterialTopTabNavigator(
{
EventsTab: {
screen: EventsResults,
navigationOptions: { tabBarLabel: 'Events' }
},
VehiclesTab: {
screen: VehiclesResults,
navigationOptions: { tabBarLabel: 'Vehicles' }
}
},
{
initialRouteName: 'EventsTab',
navigationOptions: () => ({
tabBarVisible: true
}),
swipeEnabled: true,
tabBarOptions: {
upperCaseLabel: false,
activeTintColor: Colors.defaultPrimaryColor,
inactiveTintColor: Colors.defaultPrimaryColor,
indicatorStyle: {
backgroundColor: Colors.defaultPrimaryColor,
},
tabStyle: {
height: 48,
alignItems: 'center',
justifyContent: 'center',
},
style: {
backgroundColor: Colors.primaryBackgroundColor,
},
statusBarStyle: 'light-content',
},
}
);
const filterData = (query) => {
const cars = [];
cars.push({ text: 'Chevrolet Trailblazer 2011' });
cars.push({ text: 'Chevrolet Spark 2011' });
cars.push({ text: 'Chevrolet Silverado 2011' });
cars.push({ text: 'Ford Explorer 2010' });
cars.push({ text: 'Ford Escape 2012' });
cars.push({ text: 'Ford Ecosport 2014' });
const result = (query === null || query.trim() === '') ? [] : cars.filter(car => car.text.indexOf(query) !== -1);
return result;
};
class HeaderComponent extends Component {
constructor(props) {
super(props);
this.state = { text: null };
}
render() {
const data = filterData(this.state.text);
return (
<View
style={{
flex: 0,
flexDirection: 'column',
justifyContent: 'space-between',
backgroundColor: Colors.primaryBackgroundColor
}}
>
<View
style={{
flex: 0,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center'
}}
>
<Text style={[GlobalStyle.textTitleStyle, { marginLeft: 10 }]}>search</Text>
<Icon active style={{ color: Colors.defaultPrimaryColor, margin: 10 }} name='ios-close' />
</View>
<View
style={{
flex: 0,
flexDirection: 'row',
justifyContent: 'space-evenly',
alignItems: 'center',
backgroundColor: Colors.defaultPrimaryColor,
marginLeft: 10,
marginRight: 10
}}
>
<Autocomplete
data={data}
containerStyle={styles.autocompleteContainer}
style={{ color: Colors.searchTextInput, backgroundColor: 'white' }}
onChangeText={(text) => this.setState({ text })}
renderItem={item => (
<ListItem style={{ zIndex: 900 }} onPress={() => this.setState({ text: item.text })}>
<Text>{item.text}</Text>
</ListItem>
)}
/>
</View>
</View>
);
}
}
const HeaderContainer = connect(mapStateToProps, { performSearch })(HeaderComponent);
class SearchScreen extends Component {
static router = SearchTabNavigatorStack.router;
static navigationOptions = ({ navigation }) => ({
header: <HeaderContainer />
});
render() {
return (
<SearchTabNavigatorStack navigation={this.props.navigation} />
);
}
}
export default connect(mapStateToProps, null)(SearchScreen);
It had to be this way to provide the ui visual representation that was designed (not my idea); the autocomplete component is working, the result list is displayed but the items in the list are not clickable
I supossed the zIndex property should fix this, but to no avail, and i am out of ideas
Any help or suggestions ?

Cannot read property `navigation` of undefined

The following code all works except for when I click on the last SettingsList.Item component inside of the SettingsRoute. The error I get is "Cannot read property navigation of undefined".
import React from "react";
import {
View,
TextInput,
Alert,
KeyboardAvoidingView,
Dimensions,
AsyncStorage,
ActivityIndicator,
BackHandler
} from "react-native";
import { withNavigation } from "react-navigation";
import PTRView from "react-native-pull-to-refresh";
import SettingsList from "react-native-settings-list";
import { Text, Toast, Root } from "native-base";
import {
BottomNavigation,
Surface,
Button,
List,
Headline,
Switch,
Divider,
Paragraph,
Dialog,
Portal
} from "react-native-paper";
import { Home } from "../components/";
let width = Dimensions.get("window").width;
let height = Dimensions.get("window").height;
const HomeRoute = () => (
<PTRView onRefresh={() => this.refresh}>
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<Text>Logged in:</Text>
<Card name="Hello World" color="#d50000" letterGrade="ABC" grade="123" />
</View>
</PTRView>
);
const Card = props => (
<Surface
style={{
margin: 4,
padding: 8,
backgroundColor: props.color,
elevation: 3,
justifyContent: "center",
width: width - 21,
height: height / 9
}}
>
<Text
style={{
textAlign: "right",
alignSelf: "stretch",
color: "white",
fontSize: responsiveFontSize(2.2)
}}
>
{props.grade}
</Text>
<Text
style={{
textAlign: "left",
color: "white",
fontSize: responsiveFontSize(2.6)
}}
>
{props.name}
</Text>
<Text
style={{
textAlign: "right",
alignSelf: "stretch",
color: "white",
fontSize: responsiveFontSize(2.3)
}}
>
{props.letterGrade}
</Text>
</Surface>
);
const SettingsRoute = () => (
<View style={{ flex: 1, width: width - 9, justifyContent: "center" }}>
<SettingsList backgroundColor="transparent">
<SettingsList.Header
headerText="Settings"
headerStyle={{ color: "black" }}
/>
<SettingsList.Item
itemWidth={50}
title="About/Credits"
onPress={() => Alert.alert("Test")}
/>
<SettingsList.Item
itemWidth={50}
title="Help"
onPress={() => Alert.alert("TEST")}
/>
<SettingsList.Item
itemWidth={50}
title="Logout"
onPress={() => this.props.navigation.navigate("chooseDistrict")}
/>
</SettingsList>
</View>
);
const responsiveFontSize = f => {
return Math.sqrt(height * height + width * width) * (f / 100);
};
export class Grades extends React.Component {
static navigationOptions = {
title: "Grades",
headerLeft: null,
gesturesEnabled: false
};
constructor() {
super();
this.state = {
index: 0,
isAuthed: false,
routes: [
{ key: "home", title: "Home", icon: "home"},
{
key: "settings",
title: "Settings",
icon: "settings",
}
],
visible: false
};
}
_handleIndexChange = index => this.setState({ index });
_renderScene = BottomNavigation.SceneMap({
home: HomeRoute,
settings: SettingsRoute
});
refresh() {
//get grades and repopulate tabs
//refresh
this.setState({ isAuthed: true });
}
componentWillMount() {
BackHandler.addEventListener("hardwareBackPress", function() {
return true;
});
}
pleaseLogout() {
this.props.navigation.navigate("login");
}
render() {
return (
<Root>
<BottomNavigation
styles={{ backgroundcolor: "#8499B1" }}
navigationState={this.state}
onIndexChange={this._handleIndexChange}
renderScene={this._renderScene}
/>
</Root>
);
}
}
export default withNavigation(Grades);
Do I need to pass props into the function?
Am I even defining my variables in the right place?
Thanks in advance
I made a very silly typo.
onPress={() => this.props.navigation.navigate("login")} <- this does not work (login is not capitalized)
onPress={() => this.props.navigation.navigate("Login")} <- this works!