react-native flexbox children inner elements - react-native

Using react-native. How to make text show inside (and not bellow) the background image?
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image,
TextInput,
TouchableOpacity,
AsyncStorage,
} from 'react-native';
import { Navigator } from 'react-native-deprecated-custom-components';
export default class Test extends Component {
render() {
return (
<View style={styles.container}>
<Image
source={require('../img/example.jpg')}
style={styles.backgroundImage}
blurRadius={1} />
<View style={styles.content}>
<Text style={styles.logo}> Example Text </Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
},
backgroundImage: {
flex: 1,
alignSelf: 'stretch',
width: null,
justifyContent: 'center',
},
content: {
alignItems: 'center',
},
logo: {
color: 'white',
fontSize: 40,
fontStyle: 'italic',
fontWeight: 'bold',
textShadowColor: '#252525',
textShadowOffset: { width: 2, height: 2 },
textShadowRadius: 15,
marginBottom: 20,
}
});
As you can see, in the example above, the Text appears outside the backgroundimage... Maybe is something with the flexbox but im not sure...

try with:
<Image
source={require('../img/example.jpg')}
style={styles.backgroundImage}
blurRadius={1}>
<View style={styles.content}>
<Text style={styles.logo}> Example Text </Text>
</View>
</Image>

Related

Text strings must be rendered within a <Text> component. When creating a button

I'm new to react native and trying to create a button, here is my StartScreen:
import React from 'react' import Background from '../components/Background' import AppButton from '../components/AppButton'
export default function StartScreen({ navigation }) {
return(
<Background>
<AppButton title="HEEEY!" size="sm" backgroundColor="#007bff" />;
</Background>
) }
Here is my AppButton:
import React from 'react'
import { StyleSheet, TouchableOpacity, Text } from "react-native";
export default function AppButton ({ onPress, title, size, backgroundColor }) {
return (
<TouchableOpacity
onPress={onPress}
style={[
styles.appButtonContainer,
size === "sm" && {
paddingHorizontal: 8,
paddingVertical: 6,
elevation: 6
},
backgroundColor && { backgroundColor }
]}
>
<Text style={[styles.appButtonText, size === "sm" && { fontSize: 14 }]}>
{title}
</Text>
</TouchableOpacity>
)
};
const styles = StyleSheet.create({
screenContainer: {
flex: 1,
justifyContent: "center",
padding: 16
},
appButtonContainer: {
elevation: 8,
backgroundColor: "#009688",
borderRadius: 10,
paddingVertical: 10,
paddingHorizontal: 12
},
appButtonText: {
fontSize: 18,
color: "#fff",
fontWeight: "bold",
alignSelf: "center",
textTransform: "uppercase"
}
});
Here is my Background:
import React from 'react'
import { ImageBackground, StyleSheet, KeyboardAvoidingView } from 'react-native'
import { theme } from '../core/theme'
export default function Background({ children }) {
return (
<ImageBackground style={styles.background}>
<KeyboardAvoidingView style={styles.container} behavior="padding">
{children}
</KeyboardAvoidingView>
</ImageBackground>
)
}
const styles = StyleSheet.create({
background: {
flex: 1,
width: '100%',
backgroundColor: theme.colors.surface,
},
container: {
flex: 1,
padding: 20,
width: '100%',
maxWidth: 340,
alignSelf: 'center',
alignItems: 'center',
justifyContent: 'center',
},
})
When I try to run the application in my Android phone I get the following error:
Error: Text strings must be rendered within a component.
Blockquote
But I cant figure out where this error happens. Can someone spot the mistake?
You have an unnecessary semicolon (;) in your StartScreen JSX, remove it.
export default function StartScreen({ navigation }) {
return(
<Background>
<AppButton title="HEEEY!" size="sm" backgroundColor="#007bff" />
</Background>
); }

ScrollView removing TextInput keyboard in React Native

I am using TextInput with custom clear icon to clear text. As I need to keep multiple TextInputs in screen, I used a ScrollView:
import React, {Component} from 'react';
import {
View,
Text,
ScrollView,
StyleSheet,
Image,
TouchableOpacity,
TextInput,
} from 'react-native';
export default class SuccessScreen extends Component {
constructor(props) {
super(props);
this.state = {};
}
handleRightClick() {
console.log('handleRightClick');
}
render() {
return (
<ScrollView>
<View style={styles.SectionStyle}>
<TextInput style={styles.input} />
<TouchableOpacity
style={styles.ImageStyle}
onPress={this.handleRightClick}>
<Image source={require('../../images/cross_icon.png')} />
</TouchableOpacity>
</View>
</ScrollView>
);
}
}
const styles = StyleSheet.create({
input: {
flex: 1,
borderColor: '#000000',
borderBottomWidth: 1,
height: 40,
fontSize: 15,
color: '#000000',
},
SectionStyle: {
flexDirection: 'row',
minWidth: '100%',
height: 40,
},
ImageStyle: {
height: 25,
width: 25,
marginLeft: -25,
alignContent: 'center',
resizeMode: 'stretch',
alignItems: 'center',
},
});
Without the ScrollView, handleRightClick is called, but when I use the ScrollView, it just dismiss keyboard and don't call handleRightClick.
The ScrollView has a prop keyboardShouldPersistTaps. You should set it to 'handled' so your TouchableOpacity will handle the press instead of the ScrollView.
<ScrollView keyboardShouldPersistTaps='handled'>
// ...
</ScrollView>

How put a image with auto size inside of a column using React Native?

I have a row with two-column and want to put an image in one of the columns with full size.
import React from 'react';
import { mapping, light as lightTheme } from '#eva-design/eva';
import { View, ImageBackground, Image, StyleSheet } from 'react-native';
import { ApplicationProvider, Layout } from 'react-native-ui-kitten';
import { Input, InputProps } from 'react-native-ui-kitten';
import { Text, Button } from 'react-native-ui-kitten';
const App = () => (
<ApplicationProvider
mapping={mapping}
theme={lightTheme}>
<View style={styles.box}>
<View style={{width: '50%', backgroundColor: 'powderblue'}}>
<Image
resizeMode="contain"
source={require('../ProjectName/assets/image.jpg')}
style={styles.canvas} />
</View>
<View style={{width: '50%', backgroundColor: 'skyblue'}}>
<Input />
<Button />
</View>
</View>
</ApplicationProvider>
);
const styles = StyleSheet.create({
box: {
flex: 1,
flexDirection: 'row',
width: 'auto',
maxHeight: 200,
backgroundColor: 'red',
},
canvas: {
width: '100%',
},
});
export default App;
Structure:
ApplicationProvider
View
View
Image
View
Input
Button
My image is out of the column and box
!
Please guide me to fix it.
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Image,
Text,
View,
ImageBackground
} from 'react-native';
export default class App extends Component<{}> {
render() {
return (
<View style={{flex: 1, flexDirection: 'column', backgroundColor: 'black'}}>
<View style={{flex: 1/3, height: 100, flexDirection: 'row', backgroundColor: 'red'}}>
<View style={{flex: 1/2, backgroundColor: 'yellow'}}>
<Image style={{flex: 1, width: '100%', height: 'auto'}} source={require('./assets/image.png')}/>
</View>
<View style={{flex: 1/2, backgroundColor: 'pink'}}>
</View>
</View>
</View>
);
}
}

ScrollView and justifyContent issue in React Native

import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, View, ScrollView } from 'react-native';
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<ScrollView>
<View style={styles.top} />
<View style={styles.contentContainer}>
<View style={styles.content}>
<Text>Item1</Text>
</View>
<View style={styles.content}>
<Text>Item2</Text>
</View>
<View style={styles.content}>
<Text>Item3</Text>
</View>
</View>
</ScrollView>
<View style={[styles.tabbar]} />
</View>
);
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
},
top: {
height: 346,
backgroundColor: '#0066cc',
},
contentContainer: {
flex: 1,
justifyContent: 'space-around',
},
content: {
padding: 20,
borderWidth: 1,
borderColor: 'red',
},
tabbar: {
height: 50,
backgroundColor: '#eee',
},
});
Please see attached screenshot for my issue. I'm providing a Snack link with all the code.
Note that when I remove ScrollView the items align as I need them to be but removing ScrollView is not an option.
https://snack.expo.io/SJQnkXGS7
Either give the ScrollView or the contentContainer a predefined height:
<ScrollView contentContainerStyle={styles.container}>
or
<View style={[styles.contentContainer, {height: Dimensions.get('window').height - 396}]}>

Text wrap with chevron on right in React Native ListView item renderer

I am trying to have a list view show up that looks similar to this. The problem I am trying to solve is regarding word wrapping of the label. I do have it working with the code below, but it feels like a hack and doesn't work with device rotation. There has to be a way to do it without using Dimensions and styling.
Here is what I have.
import React, { Component } from 'react';
import {
StyleSheet,
Dimensions,
TouchableOpacity,
Text,
View
} from 'react-native';
import Moment from 'moment'
import Icon from 'react-native-vector-icons/FontAwesome';
class ProjectListItemRenderer extends Component {
componentDidMount() {
//alert(Dimensions.get('window').height)
}
render() {
return (
<View style={styles.projectRow}>
<View style={{width: Dimensions.get('window').width - 50}}>
<Text style={styles.itemName}>{this.props.name}</Text>
<Text style={styles.itemDetails}>{`${Moment(this.props.lastUpdated).fromNow()}`}</Text>
</View>
<View style={styles.moreContainer}>
<Icon name="chevron-right" size={15} style={styles.moreIcon} />
</View>
</View>
);
}
}
const styles = StyleSheet.create({
projectRow: {
flexDirection: 'row',
justifyContent: 'flex-start',
padding: 15,
},
itemName: {
fontSize: 18,
color: '#4A90E2',
},
itemDetails: {
fontSize: 12,
color: '#BBBBBB',
},
moreContainer: {
justifyContent: 'center',
alignItems: 'center'
},
moreIcon: {
color: "#d6d7da"
}
});
module.exports = ProjectListItemRenderer;
The other option I tried was absolute positioning, with the label being 20px from the right, and then absolute positioning the chevron on the right. The problem I ran into there was trying to figure out the height of the individual listItem renderer after it is rendered (and word wrapped).
The problem comes from having flexDirection: 'row' in the View containing your text. This makes the text overflow to the right instead of wrapping. If you want your text to wrap, the containing View must have flexDirection: 'column' in the style.
I've modified your code accordingly:
import React, { Component } from 'react';
import {
StyleSheet,
Dimensions,
TouchableOpacity,
Text,
View
} from 'react-native';
import Moment from 'moment'
import Icon from 'react-native-vector-icons/FontAwesome';
class ProjectListItemRenderer extends Component {
componentDidMount() {
//alert(Dimensions.get('window').height)
}
render() {
return (
<View style={styles.projectRow}>
<View style={styles.projectText}>
<Text style={styles.itemName}>{this.props.name}</Text>
<Text style={styles.itemDetails>
{`${Moment(this.props.lastUpdated).fromNow()}`}
</Text>
</View>
<View style={styles.moreContainer}>
<Icon name="chevron-right" size={15} style={styles.moreIcon} />
</View>
</View>
);
}
}
const styles = StyleSheet.create({
projectText: {
flex: 1,
flexDirection: 'column'
},
projectRow: {
flexDirection: 'row',
justifyContent: 'flex-start',
padding: 15,
},
itemName: {
fontSize: 18,
color: '#4A90E2',
},
itemDetails: {
fontSize: 12,
color: '#BBBBBB',
},
moreContainer: {
justifyContent: 'center',
alignItems: 'center'
},
moreIcon: {
color: "#d6d7da"
}
});
module.exports = ProjectListItemRenderer;
The only difference is I replaced {width: Dimensions.get('window').width - 50} with {flex: 1, flexDirection: 'column'}.