React Native Can't set margin after image - react-native

Why can't I separate the last two components?
First, I tried to set justifyContent, but it did not work.
Then, I tried to set margin. It works for title and image, but not for components after Image
I think is because I'm setting image height, but without it image will not appear
import React, {useState, useEffect} from 'react';
import {View, Text, Image, StyleSheet} from 'react-native';
const Card = ({ evento }) => {
return (
<View style={styles.card_container}>
<Text style={styles.titulo}>
{evento.titulo} - R$ {evento.valor}
</Text>
<Image
style={styles.img}
source={{uri: 'https://via.placeholder.com/150'}}
/>
<Text styles={styles.descricao}>{evento.descricao}</Text>
<Text styles={styles.guia}>Organizador: {evento.guia}</Text>
</View>
);
};
const styles = StyleSheet.create({
card_container: {
// flex: 1,
// display: 'flex',
// flexDirection: 'column',
// justifyContent: 'space-between',
fontFamily: 'Rubik-Light',
margin: 3,
marginBottom: 15,
padding: 10,
borderRadius: 10,
backgroundColor: '#ffffff',
elevation: 1,
},
titulo: {
fontFamily: 'Rubik-Medium',
margin: 10,
},
img: {
//alignSelf: 'center',
width: 350, //TODO tamanho dinĂ¢mico
height: 200,
marginBottom: 10, // word
},
descricao: {
marginBottom: 10,
},
guia: {
marginTop: 10
}
});
export default Card;

It's a typo on the Text component. It should be style for the prop in Text component not styles. Here are the correct codes for the last two components.
<Text style={styles.descricao}>{evento.descricao}</Text>
<Text style={styles.guia}>Organizador: {evento.guia}</Text>

Related

ReactNative - Button Not Visible

I'm having an issue where I'm creating a simple button that isn't visible (but it is there because I'm able to trigger the onPress method) within this view, but I am able to create other components within this View that appear. My code in App.js is below, any help would be greatly appreciated!
import React from 'react';
import {
View,
StyleSheet,
Text,
TouchableOpacity,
} from 'react-native';
export default function App() {
return (
<View style={{
flex: 1,
justifyContent: "center",
alignItems: "center",
}}>
<TouchableOpacity style={styles.button} onPress={() => console.log("Clicked")}>
<Text style={styles.text}>Login</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
button: {
color: "tomato",
borderRadius: 25,
justifyContent: "center",
alignItems: "center",
padding: 15,
height: 50,
width: "100%",
},
text: {
color: "#fff",
fontSize: 18,
fontWeight: "bold",
textTransform: "uppercase",
}
})
You have a white button with white text in front of a white background so it all blends in. Look at this expo snack and the code below to see why (https://snack.expo.dev/#heytony01/thankful-juice-box)
import React from 'react';
import {
View,
StyleSheet,
Text,
TouchableOpacity,
} from 'react-native';
export default function App() {
return (
<View style={{
flex: 1,
justifyContent: "center",
alignItems: "center",
}}>
<TouchableOpacity style={styles.button} onPress={() => console.log("Clicked")}>
<Text style={styles.text}>Login</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
button: {
borderColor:"red", // give border color
borderWidth:2, // give border width
color: "tomato",
borderRadius: 25,
justifyContent: "center",
alignItems: "center",
padding: 15,
height: 50,
width: "100%",
},
text: {
color: "black", // change font to black
fontSize: 18,
fontWeight: "bold",
textTransform: "uppercase",
}
})
I was setting the color of the button instead of the backgroundColor

Why is my style not applied to my child component?

Good evening,
I have been trying to create custom card Component in react native that i can use everywhere i need it.
I apply a part of the style inside the component itself, and for the width, layout etc, where i need it. Then i am trying to use the spread operator to "merge" all the properties in the style of the child component. I saw it in a video tutorial, but it's not working for me, and i don't see what is wrong in my code :
import React from 'react';
import { View, Text, StyleSheet, TextInput, Button } from 'react-native';
import Card from '../components/Card';
import Color from '../constants/colors'
import Input from '../components/Input'
const StartGameScreen = props => {
return (
<View style={styles.screen}>
<Text style={styles.title}>Start a New Game!</Text>
<Card style={styles.inputContainer}>
<Text>Select a Number</Text>
<Input />
<View style={styles.buttonContainer}>
<Button title="Reset" color={Color.accent} onPress={() => {}} />
<Button title="Confirm" color={Color.primary} onPress={() => {}} />
</View>
</Card>
</View>
);
};
const styles = StyleSheet.create({
screen: {
flex: 1,
padding: 10,
alignItems: 'center'
},
title: {
fontSize: 20,
marginVertical: 10
},
inputContainer: {
width: 300,
maxWidth: '80%',
alignItems: 'center',
},
buttonContainer: {
flexDirection: 'row',
width: '100%',
justifyContent: 'space-between',
paddingHorizontal: 15
},
input: {
width: 50
}
});
export default StartGameScreen;
And the child component :
import React from 'react';
import { View, StyleSheet } from 'react-native';
const Card = props => {
return (
<View style={{ ...styles.card, ...props.style }}> {props.children}</View>
);
};
const styles = StyleSheet.create({
card: {
shadowColor: 'black',
shadowOffset: { width: 0, height: 2 },
shadowRadius: 6,
shadowOpacity: 0.26,
elevation: 8,
backgroundColor: 'white',
padding: 20,
borderRadius: 10
}
});
export default Card;
Note : I tried with input and card, it's working for none of them.
Is that something that we were able to do but we can't anymore ?
Thank you
Have a nice weekend
Try this:
const Card = props => {
return (
<View style={[styles.card, props.style ]}> {props.children}</View>
);
};

center text in a view on top of the screen (react-native)

I am trying to create a view with height of 200 and with some background color. on top of the view i want to place two texts in the center of the screen right below the toolbar. my code is not centering the texts. texts are appearing on the left side but i want them to be centered.
I am using Base.js style script as a way to standardize my application to reuse styles
see attached image
see code below
//Base.js
import {StyleSheet, Dimensions} from 'react-native'
export const dimensions = {
fullHeight: Dimensions.get('window').height,
fullWidth: Dimensions.get('window').width
}
export const colors = {
primary: '#039BE5',
secondary: '#027CB7',
tertiary: '#FF4081'
}
export const padding = {
sm: 10,
md: 20,
lg: 30,
xl: 40
}
export const margin = {
margin_1dp: 1,
margin_4dp: 4,
}
export const fonts = {
font_18sp: 18,
font_44sp: 44,
lg: 28,
//primary: 'Cochin'
}
export const pos = {
position: 'absolute',
top: 0,
alignItems: 'center',
justifyContent:'center',
}
// added below our colors, fonts, padding etc
// base styles
const baseStyles = {
container: {
flex: 1,
flexDirection: 'column',
alignItems: 'stretch',
},
header: {
backgroundColor: 'transparent',
position: pos.position,
top: 0,
fontSize: fonts.font_44sp,
fontFamily: fonts.primary,
fontWeight: 'bold',
color: 'black',
textAlign: 'center',
},
sub_header: {
backgroundColor: 'transparent',
position: pos.position,
top: 55,
fontSize: fonts.font_18sp,
fontFamily: fonts.primary,
fontWeight: 'bold',
color: 'white',
textAlign: 'center',
},
image: {
height: 200, justifyContent:'center', backgroundColor: '#00BCD4'
},
section: {
paddingVertical: padding.lg,
paddingHorizontal: padding.xl
},
}
export default function createStyles(overrides = {}) {
return StyleSheet.create({...baseStyles, ...overrides})
}
//This is an example code for NavigationDrawer//
import React, { Component } from 'react';
//import react in our code.
import { StyleSheet, View, Text } from 'react-native';
// import all basic components
import createStyles from '../styles/Base.js'
const styles = createStyles()
//import styles from './Style.js'
export default class Screen1 extends Component {
//Screen1 Component
render() {
return (
<View style={styles.container}>
<View style={{ height: 200, justifyContent:'center', backgroundColor: '#00BCD4' }} />
<Text style={styles.header}> $0.00 </Text>
<Text style={styles.sub_header}> Current Balance </Text>
</View>
);
}
texts should be center in the middle of the screen and below the toolbar.
can anyone help fix my code so that the texts are center? thanks in advance
The default flexDirection in react-native is column ... so you don need to specify it ... and alignItems will center children horizontally with column-flex-direction
<View
style={{ alignItems: 'center', backgroundColor: '#00BCD4', height: 200 }}
>
<Text style={styles.header}> $0.00 </Text>
<Text style={styles.sub_header}> Current Balance </Text>
</View>
So this might not solve everything but a common mistake people make when it comes to flexbox is they don't realize when they specify a flex-direction, the styles you use to center get reversed.
Typically if flex-direction was set to default which is row, you would use align-items to vertically center.
However, because you have it set to column, you need to use justify-content instead.
This is a pretty good article on flex. https://css-tricks.com/snippets/css/a-guide-to-flexbox/

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
}

Not printed state in React Native

I'm now using React Native. Now I'm trying to print out one of states that are initialized ( for instance, showText herein ). When I recall this value in render, it doesn't show up ( blank ). The way that I recall is "{this.state.showText}". It doesn't produce any error messages.
Maybe because of the same reason, the initial value of TouchableHighlight seems to be blank. I'm completely lost here since a sample app in Facebook Github works fine within the website. Please give me any idea on this matter.
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image,
ScrollView,
ListView,
TouchableHighlight,
TextInput
} from 'react-native';
class Greeting extends Component {
render() {
return (
<Text>Hello {this.props.name}! {this.props.date}~ Class can be added..{'\n'}</Text>
);
}
}
class Test1 extends Component {
constructor(props) {
super(props);
this.state = {
showText: 'true',
showTextBool: true,
wow: 'ddsdfasdf'
};
}
onSignupPress(){
return "hello";
}
render() {
let display = this.state.showTextBool ? 'true' : 'false';
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native! {this.state.showText} {this.state.showTextBool}
{this.state.wow}
</Text>
<TextInput style={styles.searchInput} value={this.state.wow} placeholder='Search via name or postcode'/>
<TouchableHighlight style={styles.button} underlayColor='#99d9f4'>
<Text style={styles.buttonText}>Go</Text>
</TouchableHighlight>
<Text style={styles.welcome}>
{display} + {this.onSignupPress()} + {this.state.showText}
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
buttonText: {
color: 'white',
alignSelf: 'center'
},
button: {
height: 36,
flex: 1,
flexDirection: 'row',
backgroundColor: '#48BBEC',
borderColor: '#48BBEC',
borderWidth: 1,
borderRadius: 8,
marginBottom: 10,
alignSelf: 'stretch',
justifyContent: 'center'
},
searchInput: {
height: 36,
padding: 4, marginRight: 5,
flex: 4,
fontSize: 18, borderWidth: 1, borderColor: '#48BBEC', borderRadius: 8, color: '#48BBEC'
}
});
AppRegistry.registerComponent('Test1', () => Test1);
I tried your code in react native express page. It renders one "true", but you are expected two (the text and the boolean). In the following part:
Welcome to React Native! {this.state.showText} {this.state.showTextBool}
The text is being rendered properly (it says indeed 'true'), but the boolean is not there, mainly because you cannot render a boolean like that. Try with the following:
{String(this.state.showTextBool)}
This will render the boolean value properly.