"View" disapear when "padding" is removed - react-native

I am studying React Native and trying to understand the behavior of the components' style.
I am just creating a simple app and testing the padding style.
Why the boxes disapear when I remove the padding style?
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<View style={styles.box} />
<View style={styles.box} />
<View style={styles.box} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
box: {
padding:20, //remove this line
margin: 4,
backgroundColor: 'steelblue'
}
});

Cause <View /> is like <div> in HTML, it's size is determined by its content, if you remove the padding, then it occupies 0 space.
One way to keep <View /> visible even if it has no content, is to set flex in style like { flex: 1 }

The Box View needs to be either of Fixed dimensions like this:
box: {
//width: 50,
//height: 50,
padding:20,
margin: 4,
backgroundColor: 'steelblue'
}
By padding it is given a fixed size, but if you remove padding it will have no size.
Or you can use Flex dimensions like this:
box: {
flex:1,
padding:20,
margin: 4,
backgroundColor: 'steelblue'
}
In this one you can safely remove padding:20, line.

Related

Expo screen not loading images after I put it in visual studio code

Everytime I try to load an image into my project, it doesn't show in expo. It shows up as blank when I check the expo screen for my image. I tried multiple times but the image is not loading on the screen. I imported the image and even typed in the call function properly. My code is:
import { StatusBar } from 'expo-status-bar'
import { StyleSheet, Text, View, Image, Button } from 'react-native';
export default function App() {
return (
<View>
<Image source = {require('./assets/logo1.png')}
style={styles.callText}></Image>
<Text style={styles.ligmaText}>Viral</Text>
<Text style ={styles.fontText}>Forgot Password?</Text>
<Text style ={styles.baseText}>Don't have an account? <Button title = "Register Now"></Button></Text>
<Text>Google</Text>
<Text>Facebook</Text>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
baseText: {
flex: 2,
marginTop: 0,
alignItems: 'strech',
},
fontText: {
flex: 10,
marginTop: 50,
textAlign: 'right',
},
ligmaText: {
flex: 3,
fontSize: 22,
},
callText: {
flex: 10,
}
});
I'm sure Image tags are self closing
<Image/>
You will need to manually specify the dimensions of your image, i.e. give image height and width in your styles.
Read here: Image - Expo Documentation

React Native - Text component breaks boundaries and doesnt scale its size; behaves differently on ios & android

I'm trying to create a component that has a minHeight, but can scale if it's content get larger. Its content is a row containing an icon + text (Idea is that when icon size is slightly bigger it wouldn't immediately increase the height of the component).
First image below shows how it should look like and how it's displayed on the web, but on iOS and android it doesnt work as expected. Seems like the height of the view doesn't scale properly and instead keeps a fixed size + the icon will make it break boundries.
Try for yourself: https://snack.expo.dev/ZBTGBMLa9
How it should look like (and how it's displayed in the webview)
How it actually looks like on iOS and android
Code from this component
import * as React from 'react';
import { Text, View, StyleSheet, Image } from 'react-native';
export default function TestComp({text, logo}) {
return (
<View style={styles.container}>
<View style={styles.content}>
{logo && <Image style={styles.logo} source={require('../assets/snack-icon.png')} /> }
<Text>
{text}
</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
minHeight: 50,
marginVertical: 10,
alignItems: 'center'
},
content: {
padding: 10,
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'green',
},
logo: {
height: 28,
width: 28,
marginRight: 8,
},
});
Just give the Text this style:
js <Text numberOfLines={1} style={{maxWidth: '90%'}} >

How to hide the react native focus input border?

There would be always a blue border when the text focus. I have already set style borderWidth: 0, but there is still a border. I also tried to set the border color, it won't work either.
Any hint?
Nothing special, I starts from expo tab boilerplate.
Also I found the autofocuse won't be able to triggered either :(
import * as React from "react";
import { StyleSheet } from "react-native";
import { TextInput } from "react-native-gesture-handler";
import EditScreenInfo from "../components/EditScreenInfo";
import { Text, View } from "../components/Themed";
export default function TabOneScreen() {
return (
<View style={styles.container}>
<Text style={styles.title}>Tab One</Text>
<View
style={styles.separator}
lightColor="#eee"
darkColor="rgba(255,255,255,0.1)"
/>
<TextInput
autoFocus={true}
placeholder="here is a input"
style={{ padding: 4, borderBottomWidth: 2 }}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
},
title: {
fontSize: 20,
fontWeight: "bold",
},
separator: {
marginVertical: 30,
height: 1,
width: "80%",
},
});
Can you try setting outlineStyle to 'none'. This will remove all border styles
<TextInput style={{outlineStyle: 'none' }} />

Native Base button with margin in style does not the text inside it

I wrote an app on react native using native base. when I try to put margin in button it will not show the text inside the button. How can I fix it?
When I delete the margin it work fine. but when i put the margin back it do not work. I search many source but no one show any solution at all.
Here is the code:
import React, { Component } from 'react';
import { Platform, StyleSheet, View, Image } from 'react-native';
import FloatLabelInput from './src/components/FloatLabelInput';
import {Button, Text} from 'native-base'
type Props = {};
export default class App extends Component<Props> {
render() {
return (
<View style={styles.container}>
<Image
style={styles.background_Image}
source={require('./media/free-blurry-background-1636594.jpg')}
/>
<Image source={require('./media/Save-the-Children-Logo.png')}
style={{height: '10%', width: '100%'}}/>
<FloatLabelInput name='Username' security='false'/>
<FloatLabelInput name='Password' security='true'/>
<Button block rounded danger
onPress={() => console.log('my first app')}
style = {styles.button_style}
// accessibilityLabel="Learn more about this purple button"
>
<Text >LOGIN</Text>
</Button>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#C0C0C0',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
background_Image: {
backgroundColor: '#ccc',
flex: 1,
position: 'absolute',
width: '100%',
height: '100%',
justifyContent: 'center',
},
button_style: {
margin: '5%'
}
});
I believe this is a bug related to percentage by referring to this issue https://github.com/facebook/react-native/issues/19164.. For alternative solution try using px
button_style: {
margin: 5
}

How to set the style attribution to fill the rest space?

As you see the image, if the three red views are already added on the parent view. Now I want to add another blue view which can fill the rest space. How can I set the style?
You can try this;
<View style={{flex:1,backgroundColor:'white'}}>
<View style={{justifyContent:'space-around'}}>
<View style={{height:50,alignSelf:'stretch',backgroundColor:'pink',margin:5}}/>
<View style={{height:50,alignSelf:'stretch',backgroundColor:'pink',marginHorizontal:5}}/>
<View style={{height:50,alignSelf:'stretch',backgroundColor:'pink',margin:5}}/>
</View>
<View style={{flex:1,alignItems:'center',justifyContent:'center',alignSelf:'stretch',backgroundColor:'blue',margin:5}}>
<Text style={{color:'white',fontWeight:'bold'}}>
View
</Text>
</View>
</View>
A much easier solution is to use the attribute flexGrow: 1 on the View you want to fill the remaining space.
flexGrow describes how any space within a container should be distributed among its children along the main axis. After laying out its children, a container will distribute any remaining space according to the flex grow values specified by its children.
flexGrow accepts any floating point value >= 0, with 0 being the default value. A container will distribute any remaining space among its children weighted by the child’s flex grow value.
https://facebook.github.io/react-native/docs/flexbox
DEMO
Code
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<View style={styles.flexContainer}>
<View style={styles.box1}></View>
<View style={styles.box2}></View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
padding: 8,
},
flexContainer: {
backgroundColor: 'black',
height: 100,
flexDirection: 'row'
},
box1: {
backgroundColor: 'blue',
width: 100,
height: '100%'
},
box2: {
backgroundColor: 'red',
height: '100%',
flexGrow: 1
}
});