React-native, render a button click dynamically - react-native

i want to generate a button click dynamically for a TouchableOpacity in react-native,
i didn't find anything about that,
all i want is to call the TouchableOpacity onPress from a fuction (or see its effect on the button)
in titanium we were doing $.button.click
i tried using Animated but no luck
https://facebook.github.io/react-native/docs/animations.html
so can anybody help? thanks in advance

It's really inadvisable but something like this should work:
simulatePress() {
this.touchable.props.onPress();
}
render() {
return (
<TouchableOpacity ref={component => this.touchable = component} onPress={() => console.log('onPress')}>
<Text>Tap me</Text>
</TouchableOpacity
);
}
Really though, what you are trying to achieve? There is likely a better way to do it.

Related

React-Native useState and navigation are interfering

Hey Im trying to save data from the textinput with useState. I also have a button which should just bring the user to the past screen. But because both are getting rendered at the time the program gives me a warning and the button is getting disabled.
It says: Cannot update a component while rendering a different component.
And I found out I have to use the useEffect hook but I dont know how to implement it. I tried but It didnt work. I think my problem is in the logic. Hope someone can help me :D
const [text, changeText] = React.useState("")
useEffect(() => {
onChangeText();
}, [] );
return(
<View>
<Text>DER NEUE SCREEN</Text>
<TextInput placeholder="Name your Year" onChangeText={changeText} />
<TouchableOpacity onPress={navigation.goBack()}><Text>BUTTON</Text></TouchableOpacity>
<Text>{text}</Text>
</View>
);
}

Swipeable modal (to close) in react native

I work with Expo.
I'm building a modal on the right of the screen, to display components.
It should appear on a button click, and close on swipe.
I made a lot of researches but I can't find what I need to use.
I'm a React developper but I am new to react native. I'm looking for "gestures" but I'm not sure I can use it to achieve what I want.
Any idea about what I should dig into ?
you can use this package to have that nice feature: react-native-swipe-gestures
you just need to wrap your modal with this package :
import GestureRecognizer from 'react-native-swipe-gestures';
<GestureRecognizer
style={{flex: 1}}
onSwipeUp={ () => this.setModalVisible(true) }
onSwipeDown={ () => this.setModalVisible(false) }
>
<Modal
animationType="slide"
presentationStyle="formSheet"
visible={ modalVisible }
>
<Text>Swipe Down Please</Text>
</Modal>
<Text>Swipe Up Please</Text>
</GestureRecognizer>

onPress in TouchableOpacity doesn't trigger

I need your help! My goal is to change the style of my button after I clicked it! I heard about direct manipulation and I decided to give it a try. Now I don't know why but the onPress inside my TouchableOpacity doesn't work. Here is the code:
<TouchableOpacity onPress={() => this.changeStyle}>
<TouchableHighlight style={styles.answer} ref="answer1">
<Text ...> Some Text </Text>
</TouchableHighlight>
</TouchableOpacity>
And here is my changeStyle function:
changeStyle() {
this.refs['answer1'].setNativeProps({
style: { backgroundColor: "#13a88a"}
});
}
Now i don't know why but the 'onPress' is never triggered.
Thank you for your answers!
If you want to execute the function by using 'this.changeStyle`, write your onPress like so:
<TouchableOpacity onPress={this.changeStyle}/>
If you're going to pass a function within the onPress prop that executes this.changeStyle write your onPress like so:
<TouchableOpacity onPress={() => this.changeStyle()}/>
P.S: Why do you have <TouchableHighlight/> inside a <TouchableOpacity/>? Just use one and add the onPress prop on it.
You need to import TouchableOpacity from react-native instead of importing it from react-native-gesture-handler. The version in react-native-gesture-handler is 100% broken. The version in react-native works.

how to create action button react native redirect other page?

i'm new in react native. i'm use react native action button and i want if button clicked, then show other page. this is my code but still doesn't work. have any solution?
render() {
return (
<View style={styles.container}>
<ActionButton buttonColor="#1E73C1" onPress={() => this.buttonPressed}>
</ActionButton>
</View>
);
}
buttonPressed() {
this.props.navigation.navigate('NewCase', {});
}
You don't execute buttonPressed at all. Fix it with ():
<ActionButton buttonColor="#1E73C1" onPress={() => this.buttonPressed()}>
Other way would be:
<ActionButton buttonColor="#1E73C1" onPress={this.buttonPressed.bind(this)}>
And like said in the comments, you should ensure that navigation actually exists in the props.
Be sure to pass the navigation prop to the component
Example
<ActionButton buttonColor="#1E73C1" onPress={() => this.buttonPressed('page2')}>
buttonPressed(page){
this.props.navigator.replace({
id: page,
})
}
You just put buttonPressed thats doesn't really do nothing if you have a buttonPressed() to exec the transition you need to put buttonPressed() , by the away a good thing to do is putting this type of function with '_' behind , like this: _buttonPressed('page2') and then _buttonPressed(page) , it helps a bit to know what you are doing

React Native Touchable is disabling ScrollView

I'm new to React Native, so am probably asking something very obvious, but please help.
I have a view wrapped in a touchable, so that the whole area responds to tapping. Then have a ScrollView nested inside the view. The overall structure is something like this:
<TouchableWithoutFeedback onPress={this.handlePress.bind(this)}>
<View>
<ScrollView>
<Text>Hello, here is a very long text that needs scrolling.</Text>
<ScrollView>
</View>
</TouchableWithoutFeedback>
When this compiles and runs, the tapping is detected, but the scroll view doesn't scroll at all. I made the above code short and simple, but each component has the proper styling and I can see everything rendering fine and the long text is cutoff at the bottom of the ScrollView. Please help.
Thank you!
This is what worked for me:
<TouchableWithoutFeedback onPress={...}>
<View>
<ScrollView>
<View onStartShouldSetResponder={() => true}>
// Scrollable content
</View>
</ScrollView>
</View>
</TouchableWithoutFeedback>
The onStartShouldSetResponder prop stops the touch event propagation towards the TouchableWithoutFeedback element.
I'm using this structure it's working for me:
<TouchableWithoutFeedback onPress={() => {}}>
{other content}
<View onStartShouldSetResponder={() => true}>
<ScrollView>
{scrollable content}
</ScrollView>
</View>
</TouchableWithoutFeedback>
You can have a scrollView or FlatList inside a TouchableWithoutFeedback. Tho you shouldn't but some times you have no other choice to go. Taking a good look at this questions and answer validates that.
close react native modal by clicking on overlay,
how to dismiss modal by tapping screen in react native.
For the Question, The only way you can make it work (atleast that i know of), or the simplest way is to add a TouchableOpacity around Text in your code like this,
<TouchableWithoutFeedback onPress={this.handlePress.bind(this)}>
<View>
<ScrollView>
<TouchableOpacity>
<Text>Hello, here is a very long text that needs scrolling.</Text>
</TouchableOpacity>
<ScrollView>
</View>
</TouchableWithoutFeedback>
Note: TouchableOpacity is a wrapper for making Views respond properly to touches so automatically you can style it the way you would have styled your View Component then set some of its special props to whatever you want e.g activeOpacity etc. Moreso you can use TouchableHighlight it works, but it receives one child element i.e you enclose all your component inside a parent one.
I'm using this structure it's working for me:
<TouchableOpacity>
{other content}
<ScrollView>
<TouchableOpacity activeOpacity={1}>
{scrollable content}
</TouchableOpacity>
</ScrollView>
I found that for my situation the other examples did not work as they disabled the ability to click or disabled the ability to scroll. I instead used:
<FlatList
data={[{key: text1 }, { key: text2 } ...]}
renderItem={({ item }) => (
<TouchableWithoutFeedback onPress={this.onPressContent}>
<Text style={styles.text}>{item.key}</Text>
</TouchableWithoutFeedback>
)}
/>
I happend to need to multiple chunks but you could use single element in the data array for one piece of text.
This let the press event to fire as well as let the text scroll.
Trying to use a ScrollView component inside a TouchableWithoutFeedback component can cause some unexpected behavior because the TouchableWithoutFeedback component is designed to capture user gestures and trigger an action, but the ScrollView component is designed to allow users to scroll through content.Here is what the official docs say
Do not use unless you have a very good reason. All elements that
respond to press should have a visual feedback when touched.
TouchableWithoutFeedback supports only one child. If you wish to have
several child components, wrap them in a View. Importantly,
TouchableWithoutFeedback works by cloning its child and applying
responder props to it. It is therefore required that any intermediary
components pass through those props to the underlying React Native
component.
Thats write , you cannot have a scroll view inside the TouchableWithoutFeedback, it the property of react native that it will disable it, you can instead have your scroll view outside the TouchableWithoutFeedback tab and add the other contents that you want upon the click inside a view tag.
You can also use the Touchable Highlights instead, if the TouchableWithoutFeedback does not works.