I'm not sure why TouchableOpacity is not working in a map.
Here is my code:
<Content>
<Form>
<Header>
<Title>{concernName}</Title>
</Header>
{ values.map(value => (
<TouchableOpacity
key={value.id}
onPress={() => console.log('pressed')}
style={styles.touchable}>
<ListItem>
<CheckBox />
<Text style={styles.bodyTextOptions}>{value.name}</Text>
</ListItem>
</TouchableOpacity>
)) }
</Form>
</Content>
To confirm the placement of the TouchableOpacity I've added the style:
touchable: {
width: '100%',
backgroundColor: '#e5edf9',
margin: 10,
height: 55
},
This produces the following:
When I click on the blue area, nothing happens. I get no error just nothing happens.
Any ideas what I'm doing wrong?
I ended up getting this to work by eliminating the TouchableOpacity. For some reason the TouchableOpacity wasn't working with a nested checkbox.
{ values.map(value => (
<ListItem key={value.id} onPress={ () => console.log('pressed') }>
<CheckBox checked={ this.activateCheckBox(item) }/>
<Body>
<Text style={styles.bodyTextOptions}>{value.name}</Text>
</Body>
</ListItem>
)) }
With the above I can check the whole row and also check the checkbox.
Please check your import statement and make sure that your TouchableOpacity is imported from react-native and not from react-native-gesture-handler because the same thing happens when you import it from react-native-gesture-handler
In such case you can use TouchableOpacity from 'react-native-gesture-handler' package instead of 'react-native' package
you missed a pair of curly braces in onPress={() => console.log('pressed')}
it should be onPress={() => {console.log('pressed')}}
what you did is to just return a meaningless value(a function), you should actually call the function with help of curly braces.
Arrow function by default return the value at right side of the arrow, and execute the codes if it's wrapped with curly braces.
CheckBox interface extends Touchableopacity props in NativeBase.
So you can call onPress in Touchableopacity as below,
<Content>
<Form>
<Header>
<Title>{concernName}</Title>
</Header>
{values.map(value => (
<TouchableOpacity
key={value.id}
onPress={() => console.log('pressed')}
style={styles.touchable}>
{/* <ListItem>
<CheckBox />
<Text style={styles.bodyTextOptions}>{value.name}</Text>
</ListItem> */}
</TouchableOpacity>
))}
</Form>
</Content>
Or you can pass onPress as a prop to CheckBox
<Content>
<Form>
<Header>
<Title>{concernName}</Title>
</Header>
{values.map(value => (
<TouchableOpacity
key={value.id}
style={styles.touchable}>
<ListItem>
<CheckBox onPress={() => console.log('pressed')} />
<Text style={styles.bodyTextOptions}>{value.name}</Text>
</ListItem>
</TouchableOpacity>
))}
</Form>
</Content>
Related
My code:
{this.state.News.slice(0, this.state.currentShow).map((item) => {
return (
<View>
<View>
<View>
<Text>{item.Title}</Text>
<Text>{item.Content}</Text>
</View>
<View>
<Image source={{uri: item.Image}} />
</View>
</View>
<View>
<Text>{item.creationTime}</Text>
<Button onPress={this.gotoNews(item.Id.toString())}>
<Text>Load More</Text>
</Button>
</View>
</View>
)
})}
Whenever this page is run, Everything is right But why the button onPress runs when the react native screen is loaded!!
Try to change your button handler call from a function call to a function
you can use ES6 arrow functions to do this :
<Button onPress={()=>this.gotoNews(item.Id.toString())}>
<Text>Load More</Text>
</Button>
I am trying design like below image with react native.If anyone have worked like this modal design over the toolbar then please help me.
1
You will need a Modal with a embedded TouchableOpacity combined with some styling for positioning.
Please refer this
https://snack.expo.io/SJrDAC8Qr
render() {
return (
<>
<View>
<Appbar.Header>
<Appbar.Content title="Title" subtitle="Subtitle" />
<Appbar.Action icon="search" onPress={() => this.setState({displayModal: true})} />
</Appbar.Header>
<View>
<Text>Main content!</Text>
</View>
</View>
{/*Modal code*/}
<Modal transparent={true} visible={this.state.displayModal}>
{/*Container .. clicking this closes the modal*/}
<TouchableOpacity style={{flex:1}} onPress={() => this.setState({displayModal:false})}>
<View style={{backgroundColor:'blue', position:'absolute', right:0, width:200, height: 200}}>
<Text style={{color:'#ffffff'}}>Hello World!</Text>
</View>
</TouchableOpacity>
</Modal>
</>
);
}
Not very nicely styled but I guess it does what you want
I know there are already so many queries on this topic, I have tried every step but still won't be able to fix the issue.
Here is the code :
render() {
const {sContainer, sSearchBar} = styles;
if (this.props.InviteState.objectForDeleteList){
this.updateList(this.props.InviteState.objectForDeleteList);
}
return (
<View style={styles.mainContainer}>
<CustomNavBar
onBackPress={() => this.props.navigation.goBack()}
/>
<View
style={sContainer}
>
<ScrollView keyboardShouldPersistTaps="always">
<TextInput
underlineColorAndroid={'transparent'}
placeholder={'Search'}
placeholderTextColor={'white'}
selectionColor={Color.colorPrimaryDark}
style={sSearchBar}
onChangeText={(searchTerm) => this.setState({searchTerm})}
/>
</ScrollView>
{this.renderInviteUserList()}
</View>
</View>
);
}
renderInviteUserList() {
if (this.props.InviteState.inviteUsers.length > 0) {
return (
<SearchableFlatlist
searchProperty={'fullName'}
searchTerm={this.state.searchTerm}
data={this.props.InviteState.inviteUsers}
containerStyle={styles.listStyle}
renderItem={({item}) => this.renderItem(item)}
keyExtractor={(item) => item.id}
/>
);
}
return (
<View style={styles.emptyListContainer}>
<Text style={styles.noUserFoundText}>
{this.props.InviteState.noInviteUserFound}
</Text>
</View>
);
}
renderItem(item) {
return (
this.state.userData && this.state.userData.id !== item.id
?
<TouchableOpacity
style={styles.itemContainer}
onPress={() => this.onSelectUser(item)}>
<View style={styles.itemSubContainer}>
<Avatar
medium
rounded
source={
item.imageUrl === ''
? require('../../assets/user_image.png')
: {uri: item.imageUrl}
}
onPress={() => console.log('Works!')}
activeOpacity={0.7}
/>
<View style={styles.userNameContainer}>
<Text style={styles.userNameText} numberOfLines={1}>
{item.fullName}
</Text>
</View>
<CustomButton
style={{
flexWrap: 'wrap',
alignSelf: 'flex-end',
marginTop: 10,
marginBottom: 10,
width: 90,
}}
showIcon={false}
btnText={'Add'}
onPress={() => this.onClickSendInvitation(item)}
/>
</View>
</TouchableOpacity> : null
);
}
**I even tried with bellow code as suggested by #Nirmalsinh **:
<ScrollView keyboardShouldPersistTaps="always" style={sContainer}>
<CustomNavBar
onBackPress={() => this.props.navigation.goBack()}
/>
<TextInput underlineColorAndroid={'transparent'}
placeholder={'Search'}
placeholderTextColor={'white'}
selectionColor={Color.colorPrimaryDark}
style={sSearchBar}
onChangeText={(searchTerm) => this.setState({searchTerm})} />
{this.renderInviteUserList()}
</ScrollView>
I have followed this article:
https://medium.com/react-native-training/todays-react-native-tip-keyboard-issues-in-scrollview-8cfbeb92995b
I have tried with keyboardShouldPersistTaps=handled also but still, I have to tap twice on my Custom Button to perform an action. Can anybody tell me what I am doing wrong inside the code?
Thanks.
You need to add give value always in keyboardShouldPersistTaps to allow user tap without closing the keyboard.
keyboardShouldPersistTaps='always'
For example:
<ScrollView keyboardShouldPersistTaps='always'>
// Put your component
</ScrollView>
NOTE: Kindly put your tappable component inside the ScrollView. Otherwise it won't work.
You can use keyboardShouldPersistTaps='handled' in a ScrollView or Scrollables like FlatList SectionList etc. and embed a TouchableWithoutFeedBack to handle the case for dismiss on outside clicks.
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<ScrollView keyboardShouldPersistTaps='handled'>
// Rest of the content.
</ScrollView/>
</TouchableWithoutFeedback>
For FlatList and SectionList you will have to handle KeyBoard.dismiss separately.
Please try this, It's working for me, it will works you also, i hope it helps...
I'm trying to make those cards clickable to redirect to another screen, but I cant figure it out
let cards = this.state.items.map(item => (
<Card key={item.id} onPress={() => Actions.dogScreen()}>
<CardItem bordered>
<Left>
<Thumbnail square source={item.image ? { uri: "data:image/jpeg;base64," + item.image } : logo} />
<Body>
<Text>Name: {item.name}, Age: {item.age}</Text>
<Text note>Gender: {item.gender.name} Race: {item.race.name}</Text>
</Body>
</Left>
</CardItem>
</Card>))
You can make <Card /> clickable by wrapping the whole card by TouchableOpacity. Also don't forget to add pointerEvents="none" for each card.
import { TouchableOpacity } from 'react-native';
let cards = this.state.items.map(item => (
<TouchableOpacity key={item.id} onPress={() => Actions.dogScreen()}>
<Card pointerEvents="none">
<CardItem bordered>
<Left>
<Thumbnail square source={item.image ? { uri: "data:image/jpeg;base64," + item.image } : logo} />
<Body>
<Text>Name: {item.name}, Age: {item.age}</Text>
<Text note>Gender: {item.gender.name} Race: {item.race.name}</Text>
</Body>
</Left>
</CardItem>
</Card>
</TouchableOpacity>
))
All i need is to trigger onPress method of the ActionButton Without tapping on it.
Here is my code:
render () {
// act = this.refs.abc;
// alert(this.state.activeState)
// this.handleAutoPress();
return (
// <View style={styles.container}>
<View style={{flex:1, backgroundColor: '#f3f3f3'}} >
<Text>Component10 Component</Text>
{/*Rest of App come ABOVE the action button component!*/}
<ActionButton ref="abc" buttonColor="rgba(231,76,60,1)" >
{/*<ActionButton.Item buttonColor='#9b59b6' title="New Task" onPress={() =>
console.log("notes tapped!")}>
<Icon name="android-create" style={styles.actionButtonIcon} />
</ActionButton.Item>*/}
<ActionButton.Item buttonColor='#3498db' title="Notifications" onPress={() => {}}>
<Icon name="md-done-all" style={styles.actionButtonIcon} />
</ActionButton.Item>
<ActionButton.Item buttonColor='#1abc9c' title="All Tasks" onPress={() => {}}>
<Icon name="md-done-all" style={styles.actionButtonIcon} />
</ActionButton.Item>
</ActionButton>
</View>
// </View>
)
}
You probably want to follow the instructions in this answer: How can I trigger a tabItem onPress within a component using React Native?
Instead of trying to invoke the onPress itself, just set onPress prop to a function and invoke that function wherever needed. No need to mess with refs or anything like that.