React-Native Navigator not rendering - android

I am trying to create routing between scenes using Navigator on React-Native (Android), However Navigator won't render the scene, Here is a simple snippet I've created.
<View>
<Navigator
initialRoute={{ name: 'LoginView' }}
renderScene={(route, naigator) => {
<Text styles={{fontSize: 20, fontWeight: 'bold'}}>Hello, World!</Text>
}}
/>
</View>
This should render the text Hello, World!, but a plain white screen showed up on the emulator.

You can use the Navigator component like this:
<Navigator
style={{flex: 1}}
ref={thisRef => {this.navigator = thisRef}}
initialRoute={{ title: 'Awesome Scene', index: 0}}
renderScene={(route, navigator) => {
if(route.index == 0) {
return <View>
<Text>Hello, World!</Text>
</View>
}
if(route.index == 1) {
return <View>
<Text>Hi, There</Text>
</View>
}
if(route.index == 2) {
return <View>
<Text>Hi, There...again!</Text>
</View>
}
}} />
That way you can go to another page using
this.navigator.push({title: "Awesome Scene 2", index: 1})
or
this.navigator.push({title: "Awesome Scene 3", index: 2})
and it will render the correct scene.
Hope this helps!

Just figured out why this happens, Since renderScene takes in a function it must return a single object of react native UI. So the UI code must be wrapped inside View as seen in these examples below:
<Navigator
initialRoute={{ title: 'Awesome Scene'}}
renderScene={(route, navigator) => {
<View>
<Text>Hello, World!</Text>
<Text>Hi, There</Text>
</View>
}}
/>

Related

react-native webview in Expo App shows 'about:blank' on Android before exiting the APP with BackHandler

Good day everyone,
I have a problem with an App that gives me some headaches for couple days.
The App consists of a WebView containing a PHP WebShop and a bar-code scanner. So far everything was working as expected.
For better Navigation on Android I wanted to use the HardwareBackButton to go back in the WebView History. This also works well but has a major flaw.
I want the back button to go back until it cant go back anymore and instead it should exit the App.
Now to the actual problem: the last Page url is 'about:blank' and results in a blank screen. From here the canGoBack state is false and the App exits as expected but if you open the App again its still in the 'about:blank' state.
My first workaround was to add this.WEBVIEW_REF.current.goForward(); before BackHandler.exitApp;. This helps to get the App back in a navigateable state after exiting but i would like to completely get rid of the blank screen.
I Hope the code below is enough to understand what i wanted to achive:
componentDidMount() {
BackHandler.addEventListener('hardwareBackPress', this.handleBackButton);
}
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton);
}
handleBackButton = () => {
if (this.state.canGoBack) {
this.WEBVIEW_REF.current.goBack();
return true;
}else{
this.WEBVIEW_REF.current.goForward();
BackHandler.exitApp;
};
}
render() {
<View style={styles.container}>
<CustomStatusBar backgroundColor="#ee7203" barStyle="light-content" />
<View style={styles.main}>
<WebView
onLoad={() => this.hideSpinner()}
cacheEnabled={false}
source={{
uri: this.state.webshopURL,
}}
ref={this.WEBVIEW_REF}
onNavigationStateChange={(navState) => {
console.log(navState);
this.setState({
canGoBack: navState.canGoBack,
currentUrl: navState.url,
});
}}
allowsBackForwardNavigationGestures={true}
incognito={true}
onMessage={(event) => {
this.handleWebMassage(event.nativeEvent.data);
}}
/>
{this.state.scannerOpen === true && (
<View style={styles.barcodeScannerContainer}>
<BarCodeScanner
onBarCodeScanned={
this.state.scanned ? undefined : handleBarCodeScanned
}
style={[StyleSheet.absoluteFill, styles.barcodeScanner]}
>
<BarcodeMask edgeColor="#62B1F6" showAnimatedLine={false}/>
<TouchableOpacity
onPress={() => this.closeScanner()}
style={styles.cancel}
light
>
<Icon name="close" color="#58585a" />
</TouchableOpacity>
</BarCodeScanner>
</View>
)}
</View>
{this.state.showSplash === true && (
<View style={styles.splash}>
<Animated.Image
onLoad={this.onLoad}
{...this.props}
style={[
{
opacity: this.state.opacity,
transform: [
{
scale: this.state.opacity.interpolate({
inputRange: [0, 1],
outputRange: [0.85, 1],
}),
},
],
},
this.props.style,
]}
style={styles.logo}
source={logo}
resizeMode={"contain"}
/>
</View>
)}
</View>
I found a workaround for the Problem. The App has a SplashScreen on start and I simply set the state of canGoBack to false and am clearing the WebViews history when the SplashScreen closes.
this.sleep(5000).then(() => {
this.setState({
showSplash: false,
canGoBack: false,
});
this.WEBVIEW_REF.current.clearHistory();
});

react native camera wont work on second invokation

React native camera won't work on second invokation in Android. On the second invokation the app crashes.
I am using
react-native: 0.56.0
react-native-camera: 1.6.4
It's the same when I use latest version of the react-native-camera.
I can't update react-native to the latest because I have another package that is not compatible with the latest one and It's work fine on the emulator, only problem with real devices
The --scan result is
My camera screen
class CameraScreen extends Component {
constructor(props) {
super(props);
this.state = {
boltIconIsPressed: false,
};
}
renderError() {
Alert.alert(
'Error',
'Something went wrong, Try again!',
[
{ text: 'Ok', style: 'cancel' },
],
);
this.props.navigation.goBack();
}
render() {
if (this.props.isFocused) {
return (
<View style={styles.container}>
<RNCamera
ref={ref => {
this.camera = ref;
}}
style={styles.preview}
type={RNCamera.Constants.Type.back}
flashMode={this.state.boltIconIsPressed ? RNCamera.Constants.FlashMode.off : RNCamera.Constants.FlashMode.on}
onMountError={this.renderError.bind(this)}
permissionDialogTitle={'Permission to use camera'}
permissionDialogMessage={'We need your permission to use your camera phone'}
/>
<View
style={{ flex: 0,
flexDirection: 'row',
justifyContent: 'center',
backgroundColor: 'transparent' }}
>
<Button
outline
rounded
style={styles.capture}
onPress={() => this.props.navigation.navigate('gallery')}
>
<Icon
type='Entypo'
name='image'
style={{ color: '#862d59', }}
/>
</Button>
<Button
outline
rounded
onPress={this.takePicture.bind(this)}
style={styles.capture}
>
<Icon
type='SimpleLineIcons'
name='camera'
style={{ color: '#862d59', }}
/>
</Button>
<Button
outline
rounded
style={styles.capture}
onPress={() => this.setState({ boltIconIsPressed:
!this.state.boltIconIsPressed })}
>
<Icon
type='MaterialCommunityIcons'
name={this.state.boltIconIsPressed ? "flash-off" : "flash"}
style={{ color: '#862d59', }}
/>
</Button>
</View>
</View>
);
}
return (
<View />
);
}
takePicture = async function () {
let data = null;
if (this.camera) {
const options = {
width: 1800,
base64: true,
};
console.log(data);
data = await this.camera.takePictureAsync(options);
this.props.navigation.navigate('uploadscreen', {
image: data,
});
}
};
}
export default withNavigationFocus(CameraScreen);
Thanks in advance
I got this error because I doesnot have the storage to save the taken image. To make it work, add this in AndroidManifest.xml file
android:largeHeap="true"

Click event on words react native

I want to set different click listeners on different words of . Currently what i have is
<Text>Android iOS React Native<Text>
Now i want to know when user click on Android, iOS and React Native, i have to perform some analytics on that so need click listeners for seperate words.
Does any one have idea about it? I have checked this thread but i din't found it useful for my requirement.
Update
String i have given is just an example string, in real time i will be getting dynamic strings.
This is what i will be getting as dynamic string
{
"str":"Hi i am using React-Native, Earlier i was using Android and so on"
"tagWords":["React-Native","Android"]
}
And in output i want, "Hi i am using React-Native, Earlier i was using Android and so on"
with click event on "React-Native" and "Android". Is is possible?
The post you sent is the simplest way you can achieve the desired behavior. Sinse you need to have different listeners you need to implement different Text components.
Example
export default class App extends Component {
onTextPress(event, text) {
console.log(text);
}
render() {
return (
<View style={styles.container}>
<Text>
<Text onPress={(e) => this.onTextPress(e, 'Android')} style={styles.red}>{'Android '}</Text>
<Text onPress={(e) => this.onTextPress(e, 'iOS')} style={styles.purple}>{'iOS '}</Text>
<Text onPress={(e) => this.onTextPress(e, 'React Native')} style={styles.green}>{'React Native'}</Text>
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center'
},
red: {
fontSize: 20,
color: 'red'
},
purple: {
fontSize: 20,
color: 'purple'
},
green: {
fontSize: 20,
color: 'green'
}
});
Update 1 (Dynamic text)
render() {
const fixedString = 'I\'m a fixed string that slipleted';
const arrayOfStrings = ['These', 'are', 'strings', 'from', 'array'];
return (
<View style={styles.container}>
<Text style={styles.textContainer}>
{
fixedString.split(' ').map((str, index) => {
return (
<Text onPress={(e) => this.onTextPress(e, str)}>
{`${str}${index !== (fixedString.split(' ').lenght -1) && ' '}`}
</Text>
)
})
}
</Text>
<Text style={styles.textContainer}>
{
arrayOfStrings.map((str, index) => {
return (
<Text onPress={(e) => this.onTextPress(e, str)}>
{`${str}${index !== (arrayOfStrings.lenght -1) && ' '}`}
</Text>
)
})
}
</Text>
</View>
);
}
Update 2 (for example dynamic data)
removePunctuation = (text) => {
// this is a hack to remove comma from the text
// you may want to handle this different
return text.replace(/[.,\/#!$%\^&\*;:{}=\_`~()]/g,"");
}
render() {
const arrayOfObjects = [{
str: 'Hi i am using React-Native, Earlier i was using Android and so on',
tagWords: ['React-Native', 'Android']
}];
return (
<View style={styles.container}>
<Text style={styles.textContainer}>
{
arrayOfObjects.map((obj) => {
return obj.str.split(' ').map((s, index) => {
if ( obj.tagWords.indexOf(this.removePunctuation(s)) > -1 ) {
return (
<Text onPress={(e) => this.onTextPress(e, s)} style={styles.red}>
{`${s} ${index !== (obj.str.split(' ').lenght - 1) && ' '}`}
</Text>
)
} else return `${s} `;
})
})
}
</Text>
</View>
);
}
all you need to use is TouchableOpacity(for the tap effect and clicks), View for the alignment of texts. and certain styling. I am providing you the code snippet that will work for you , all other syntax will remain same
import {Text, View, TouchableOpacity} from 'react-native'
<View style={{flexDirection:'row'}}>
<TouchableOpacity onPress={{()=>doSomethingAndroid()}}>
<Text>Android</Text>
</TouchableOpacity>
<TouchableOpacity onPress={{()=>doSomethingiOS()}}><Text> iOS</Text>
</TouchableOpacity>
<TouchableOpacityonPress={{()=>doSomethingReactNative()}}><Text> React Native</Text>
</TouchableOpacity>
</View>
i hope this works, comment back if any issue happens
You can wrap each clickable words into 'TouchableOpacity' component, and tract the onPress event as follows
<View style={{flexDirection: 'row'}}>
<TouchableOpacity onPress={() => {
console.log('Android Clicked');
}}>
<Text>Android</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => {
console.log('iOS Clicked');
}}>
<Text>Ios</Text>
</TouchableOpacity>
</View>
Please do adjust the spacing between words.
Edit:
For dynamic string you can proceed as follows
...
handleWordClick(str, handler) {
var words = str.split(' '), // word separator goes here,
comp = [];
words.forEach((s, ind) =>{
comp.push(
<TouchableOpacity key={ind} onPress={() => handler.call(this, s)}>
<Text>{s}</Text>
</TouchableOpacity>
);
})
return comp;
}
render() {
var comp = this.handleWordClick('Android iOS React-Native', (word) => {
//handle analytics here...
console.log(word);
});
return (
<View>
...
<View style={{flexDirection: 'row'}}>
{comp}
</View>
...
</View>
)
}
I am not sure what will be your word separator as the example you have given has 'React Native' as single word. Please pay attention on this part.
Hope this will help you.

react-native-navigation showInAppNotification blank

Trying use this.props.navigator.showInAppNotification
this.props.navigator.showInAppNotification({
screen: "example.InAppNotification",
passProps: {
title: 'Title',
body: 'Body...'
},
position: 'bottom',
autoDismissTimerSec: 3
});
screen InAppNotification is registered, but see notification with blank content:
Can anyone help with this?
example.InAppNotification:
export default class InAppNotification extends Component {
render() {
console.log(this.props);
return (
<View>
<Text>
{JSON.stringify(this.props)}
</Text>
</View>
);
}
}
try by adding flex:1 to container View
export default class InAppNotification extends Component {
render() {
console.log(this.props);
return (
<View style={{ flex: 1 }}>
<Text>
{JSON.stringify(this.props)}
</Text>
</View>
);
}
}
also you need to register InAppNotification screen correctly

Platform specific components in React Native

I'm pretty sure this is straightforward, but I can't quite see how to bring it together. At the moment my app works perfectly in iOS, but I've used a few controls which are not Android compatible:
<View style={containerStyle}>
<Text style={labelStyle}>Drink:</Text>
<SegmentedControlIOS
tintColor={styleBackground}
style={{ flex: 2 }}
values={['Value1', 'Value2']}
selectedIndex={this.state.drink}
onChange={(event) => {
this.setState({ drink: event.nativeEvent.selectedSegmentIndex });
}}
/>
<View style={{ flex: 1 }} />
</View>
I want to use the React-Native-Segmented-Android library to fix this. I feel like I should be able to do something like:
<View style={containerStyle}>
<Text style={labelStyle}>Drink:</Text>
const Component = Platform.select({
ios: () => require('SegmentedControlIOS'),
android: () => require('react-native-segmented-android'),
})(
tintColor={styleBackground}
style={{ flex: 2 }}
values={['Value1', 'Value2']}
selectedIndex={this.state.drink}
onChange={(event) => {
this.setState({ drink: event.nativeEvent.selectedSegmentIndex });
}}
/>);
<View style={{ flex: 1 }} />
</View>
but that (perhaps unsurprisingly) doesn't work. Can anyone point me to the correct method? I know I can just use two different files for iOS/Android but I'd rather keep it together in one if possible.
I would create a sepeare component and this component would return segment according to platform but you can create an inner function as an alternative. call this function in render to handle decide which platform app runs and return segment according to platform.
_segmentPicker() {
if (Platform.OS == 'android') {
return (
<SegmentedControlIOS
tintColor={styleBackground}
style={{ flex: 2 }}
values={['Value1', 'Value2']}
selectedIndex={this.state.drink}
onChange={(event) => {
this.setState({ drink: event.nativeEvent.selectedSegmentIndex });
}}
/>
);
} else if (Platform.OS == 'ios') {
return (
<SegmentedControlIOS
tintColor={styleBackground}
style={{ flex: 2 }}
values={['Value1', 'Value2']}
selectedIndex={this.state.drink}
onChange={(event) => {
this.setState({ drink: event.nativeEvent.selectedSegmentIndex });
}}
/>
);
}
}
render(){
return (
<View>
{this._segmentPicker()}
.
.
</View>
);
}

Categories

Resources