Getting an entire black screen when running react-native run-ios - reactjs

When running the code an emulator for an IOS phone is produced. The application launches but produces an entire black screen. The only way to see the text is by changing the color of the text. My code is seen in the bottom.
import React from "react";
import { AppRegistry, Text } from "react-native";
const App = () => {
return <Text style={{ color: "red" }}>Some Text</Text>;
};
AppRegistry.registerComponent("albums", () => App);
From my understanding. The initial default should be a white background and the text should be in black. Hope someone can help me better understand what the issue might be. This is my first attempt with React Native although I do have some experience with React.
Thank you

You need to wrap text in a View like
<View style={{backgroundColor: 'white'}}>
<Text style={{ color: "red" }}>Some Text</Text>
</View>
As for the default style being black text on white background, that's the case if you use react-native-init or Expo to generate the project.

Related

How do I work with background in React Native?

In my app, I want to create some sort of parallax effect, just like this, where the content overlap the background : video
This is what I made in Figma: figma prototype
And this is what I currently have : current prototype
How do I allow the content to go over the picture and how am I supposed to only have the bottom of the picture for the background ? (I don't want to cut it since it could mess on other devices)
Here is my code :
import React from 'react'
import {ImageBackground,View, ScrollView, Text, StyleSheet, Dimensions} from 'react-native'
import {generalStyles} from '#gym-app/styles/general'
export default function MuscleScreen() {
const Image = require('../assets/muscles/abs.jpg')
return (
<ScrollView>
<ImageBackground source={Image} style={styles.background}>
<View style={[generalStyles.container, styles.overlap]}>
<Text style={generalStyles.title}>Abdominaux</Text>
</View>
</ImageBackground>
</ScrollView>
)
}
const styles = StyleSheet.create({
background: {
width: Dimensions.get('window').width,
height: (Dimensions.get('window').width / 16 * 9),
resizeMode: 'cover'
},
overlap: {
position: 'absolute',
top: (Dimensions.get('window').width / 16 * 9),
width: Dimensions.get('window').width
}
})
Thank you for your help !
Put your ImageBackground behind the ScrollView, e.g.
<View>
<ImageBackground />
<ScrollView />
<View>
Give ScrollView absolute position taking entire screen
As a first item in ScrollView, add blank space (empty View) with height of a
window you want for the picture
Capture current scroll position of
ScrollView with Animated.event
Interpolate that value so that it scrolls up slightly slower than ScrollView and apply it as transform translateY of your ImageBackground

Is there a way to wrap background color around text in react-native?

Currently, this is what I am trying to avoid. As you can see the background color runs until the end of the width. It would be nice for the background color to surround the text.
I looked a tons of examples and I don't really see where they wrap background color around the text itself
<Text style={{backgroundColor:'blue'}}> TTeexxtt </Text>
I tried with flexWrap and it doesn't work just like so
<Text style={{backgroundColor:'blue', flexWrap:'wrap'}}> TTeexxtt </Text>
As always, thank you
Views in react native default to an alignItems prop of stretch. What does this mean though?
All children of your view will stretch to fill the width of their parent, with considerations for margin.
If you set the alignItems property of your parent view to something other than stretch, such as center, flex-start, or flex-end you will see the background color only extend around your actual text.
You can also use alignSelf on your Text views to adjust individual items.
Here is an example snack
https://snack.expo.io/#ajthyng/vengeful-celery
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
const BGText = props => {
const { background } = props;
return <Text style={{backgroundColor: background, ...props.style}}>{props.children}</Text>;
}
export default class App extends React.Component {
render() {
return (
<View style={{flex: 1, backgroundColor: 'white', alignItems: 'stretch', marginTop: 23}}>
<BGText background='pink' style={{marginRight: 16}}>I am some text</BGText>
<BGText background='orange' style={{alignSelf: 'flex-start', marginLeft: 16}} >My BG Color is short</BGText>
</View>
);
}
}

How do i make a TouchableOpacity wraping a camera clickable on React Native android?

I am rendering a camera view inside the react-native TouchableOpacity, how do I make it clickable?
The same version of code works well on iOS, the touchableOpacity is clickable and produces the right output
<TouchableOpacity style={{width:'100%', height:300}} onPress={() =>alert("hey")}>
<Camera
style={{ height: 300, width: '100%', display: this.state.camera }}
type={this.state.type}
autoFocus={'on'}
ratio={'4:3'}
focusDepth={0}
ref={(ref) => { this.camera = ref }}>
</Camera>
</TouchableOpacity>
I expect the output to be an alert with "hey" when I press the TouchableOpacity instead I get nothing on android but I get a "hey" on iOs
That's because TouchableOpacity behavior diverges between iOs and Android. A quick fix would be just to replace the TouchableOpacity with TouchableWithoutFeedback in Android. Here's a way to do it:
const Touchable = Platform.select({ ios: TouchableOpacity, android: TouchableWithoutFeedback });
Then just use this constant to wrap your Camera view.
PS: Make sure you imported TouchableOpacity, TouchableWithoutFeedback and Platform from react-native module.

Create multiple curve points in react native component

I am currently developing a react native app, and for my login screen it has a background that has two points where the component changes its curve. Attached is the image as it can better show what it looks like then I can explain it. I was wondering if it would be possible to recreate this screen in React Native. I have access to react-native-svg but I am using expo.
As you can see, there is two curves to the blue background/component part of the screen. (This is a mockup created in Figma, not yet implemented in an App) How would I go about designing this in react native?
To use SVGs you have to use react-native-svg. Expo has it built in, though you can install it in any react-native package. You can read more about react-native-svg here.
It is fairly straight forward to use the library. As you already have a path for the SVG you can just use the Path property to draw the path on the screen.
import * as React from 'react';
import { Text, View, StyleSheet, Dimensions, TextInput } from 'react-native';
import { Constants, Svg } from 'expo';
const WIDTH = Dimensions.get('screen').width;
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<Svg height={300} width={WIDTH}>
<Svg.Path
d="M-17.5 378.5C31.5 32.5 302.5 463 375 89C447.5 -285 375 644 375 644H0C0 644 -66.5 724.5 -17.5 378.5Z" // put your path here
fill="blue"
stroke="blue"
/>
</Svg>
<View style={{backgroundColor: 'blue', flex: 1}}>
<View style={{width: WIDTH - 60, height: 60, backgroundColor:'white', borderRadius: 30, margin: 30, justifyContent: 'center', paddingLeft: 10}}>
<TextInput
placeholder='email'
/>
</View>
</View>
</View>
);
}
}
You can see it working in the following snack https://snack.expo.io/#andypandy/svg-example
This is what it looks like on an iPhone X
Umm I would do this few different way such a svg (react-native-svg) but why do that much hard work when this can be achievable just using an background image.
Use ImageBackground (https://facebook.github.io/react-native/docs/imagebackground) and fix this easily. Then add the logo and the login container as children.
Let me know if this works for you. :)
EDIT:
You might want to look at https://github.com/vault-development/react-native-svg-uri as well.

React native flex issue

I am trying to setup my child component with simple view as follow:
render() {
console.log("in render")
return (
<View style={{flex:1, backgroundColor: 'skyblue'}}>
<Text>hello</Text>
</View>
It dosnt display anything. If I remove the flex it show the "hello" text with the background color as specified. what have I done wrong?
I also using navigator... maybe I need to setup something in navigator to display the view with flex?
Try adding width: '100%' to the view style.

Resources