React Native Image not Rendering - ios

I am building a simple React Native app in which if you search a book, it will find the google books image link stored in a database for that book and map it. However, I am getting an issue. The images aren't rendering on the app, and I've tried multiple solutions to fix this. None of them worked. Please help.
Here is my code:
class Search extends Component {
constructor(props) {
super(props);
this.state = {
search: '',
loading: false,
goBack: false,
results: [],
};
}
render() {
return (
<ScrollView style={{ backgroundColor: '#fff' }}>
<Text></Text>
<Text></Text>
<View style={{ justifyContent: 'center', alignItems: 'center' }}>
<Text style={{ fontSize: 30, marginTop: 20, textAlign: 'center' }}>
Search
</Text>
<TextInput
value={this.state.email}
placeholder="Search For Books"
autoCapitalize="none"
selectionColor="black"
underlineColor="black"
underlineColorAndroid="black"
style={{
marginTop: 50,
fontSize: 20,
width: '75%',
marginBottom: 25,
}}
onChangeText={(e) => {
this.setState({ search: e });
}}></TextInput>
<Button
//icon="login"
mode="outlined"
loading={this.state.loading}
color="black"
onPress={() => {
axios
.post('https://diversitylibrary.herokuapp.com/books/search', {
query: this.state.search,
})
.then((res) => {
console.log(res.data.results.map((obj) => obj));
this.setState({
results: res.data.results.map((obj) => obj),
});
})
.catch((err) => {
console.log(err);
alert(
'Something went wrong. Are you connected to the internet?'
);
});
}}
contentStyle={{
width: 175,
height: 50,
justifyContent: 'center',
alignItems: 'center',
}}>
Search {'->'}
</Button>
<View>
{this.state.results.map((result) => (
<ScrollView>
<Image
key="bookCover"
style={{ width: 200, height: 300 }}
source={{ uri: result.cover }}></Image>
<Text>{result.title}</Text>
</ScrollView>
))}
</View>
</View>
<Text></Text>
<Text></Text>
<Text></Text>
<Text></Text>
</ScrollView>
);
}
}
export default Search;
Thanks. (Let me know if you need to see any other files.)

Try using require like this:
source={require{ uri: result.cover }}

Related

React Native Expo App Crashing due to Handmade component inside ScrollView

I am building a react native expo application. The backend is seperated and not used in the process. Below you ll see the home screen component returns
return (
<ImageBackground source={require("../assets/background.jpeg")} style={styles.background}>
<MainPageTopBar />
<View style={styles.container}>
<ScrollView>
<View style={styles.textArea}>
<Text style={styles.text} >Merhaba</Text>
</View>
<View style={styles.welcome}>
<MainCard text={"Tüm Sanatçıları Keşfet"}/>
</View>
<View>
<Text>Öne Çıkan Sahneler</Text>
<View>
<ScrollView horizontal={true}>
{
venueList.map((val, index) => {
return (
<StageCard stageName={val.name} stageLocation={val.location}/>
)
})
}
</ScrollView>
</View>
</View>
</ScrollView>
</View>
<AppBar />
</ImageBackground>
);
Likewise below u can see the code for the class StageCard
import { View, TouchableWithoutFeedback, StyleSheet, Text, ImageBackground } from "react-native";
import { Ionicons } from '#expo/vector-icons';
import React from "react";
function StageCard(props) {
const func = () => {
props.navigation.navigate(props.nav);
}
return (
<TouchableWithoutFeedback onPress={() => console.log("ola")}>
<View style={styles.container}>
<ImageBackground style={styles.stageCard} imageStyle={styles.imageStyle} source={require("../assets/venue.jpg")}>
<View style={styles.nameView}>
<Text style={styles.text}>
{props.stageName}
</Text>
</View>
<View style={styles.cityView}>
<Text style={styles.textCity}>
{props.stageLocation}
</Text>
</View>
</ImageBackground>
</View>
</TouchableWithoutFeedback>
);
}
const styles = StyleSheet.create({
container: {
width: "40%",
height: "30%",
marginLeft: "5%",
marginTop: "10%",
},
imageStyle: {
borderRadius: 20,
},
stageCard: {
width: '100%',
height: '100%',
flex: 1,
},
nameView: {
width: "50%",
height: "20%",
alignContent: "center",
backgroundColor: "transparent",
position: "absolute",
top: "5%",
left: "5%",
},
cityView: {
width: "50%",
height: "20%",
alignContent: "center",
backgroundColor: "transparent",
justifyContent: "flex-end",
position: "absolute",
bottom: "5%",
right: "5%",
},
text: {
textAlign: "left",
textAlignVertical: "center",
fontSize: 20,
color: "#f4eeeb",
borderColor: "#f4eeeb",
},
textCity: {
textAlign: "right",
textAlignVertical: "center",
fontSize: 20,
color: "#f4eeeb",
borderColor: "#f4eeeb",
},
})
export default StageCard;
The app crashes when the app is built. I have narrowed down the possibilities as when I changes the stagecard to a view, the app builds and runs too. Hence the issue must be in the StageCard component.
After coming to the prior conclusion, I tried seperatly isolating the component. Couldn't figure out what caused the issue on the ios.
Update: The problem is caused by the scrollview as the mapping separately works. I got rid of the scrollview horizontal and the cards showed up. Any ideas?

React Native KeyboardAvoidingView not working properly

I am trying to use the KeyboardAvoidingView with behavior="padding".
When I am trying to enter any text in TextInput, the TextInput field is not moving up. I have added a small view in the end which is moving up but the the view above it.
I have also with KeyboardAvoidingView height property with offset. It was working few components like 2 TextInputs. But when I add all the components the UI goes insane and behave jumbled up.
Any any idea whats happening over here?
Here the link of tutorial which I have followed.
render() {
return (
<View style={styles.backgroundContainer}>
<Loader
loading={this.state.isLoading} />
<KeyboardAvoidingView
keyboardVerticalOffset={10}
style={styles.mainContainer}
behavior='padding' >
<View style={styles.formContainer}>
<View style={[styles.centerContainer, { marginTop: 40 }]}>
<Image source={require('./../../Resources/logo.png')} />
<Text style={{ fontWeight: 'bold', color: 'gray', fontSize: 25 }}>AppName</Text>
<Text style={styles.loginMsg}> Login to your Account </Text>
</View>
<View style={styles.inputFieldsContainer}>
<Image style={{ width: 30, height: 30, margin: 5 }} source={require('./../../Resources/logo.png')} />
<TextInput
placeholder='Email'
returnKeyType='next'
keyboardType='email-address'
onChangeText={(value) => this.setState({ userEmail: value })}
onSubmitEditing={() => this.passwordInput.focus()}
style={styles.inputFields} />
</View>
<View style={styles.inputFieldsContainer}>
<Image style={{ width: 30, height: 30, margin: 5, }} source={require('./../../Resources/logo.png')} />
<TextInput
returnKeyType='go'
secureTextEntry
placeholder='Password'
ref={(input) => this.passwordInput = input}
onChangeText={(value) => this.setState({ password: value })}
style={styles.inputFields} />
</View>
<View style={styles.buttonContainer}>
<Button
fontSize='8'
color='gray'
title='Remember Me'
onPress={() => {
console.log('Remember Me Clicked');
}} />
<Button
color='gray'
title='Forgot Password?'
onPress={() => {
console.log('Forgot Password Clicked');
}} />
</View>
<TouchableOpacity
style={styles.buttonLogin}
onPress={() => {
console.log('Login Clicked');
}}
>
<Text style={{ fontSize: 20, fontWeight: 'bold', color: 'white' }}>Login</Text>
</TouchableOpacity>
<View style={{ height: 1, backgroundColor: 'gray', marginTop: 20, marginBottom: 1 }}>
</View>
<View style={[styles.centerContainer, { marginBottom: 10 }]}>
<Text style={styles.loginMsg}>Don't have a Account</Text>
<TouchableOpacity
style={styles.buttonRegister}
onPress={() => navigate('Register')} >
<Text style={styles.buttonRegisterText}>REGISTER NOW</Text>
</TouchableOpacity>
</View>
</View>
<View style={{ height: 10, backgroundColor: '#628499', }}>
</View>
</KeyboardAvoidingView>
</View>
);
}
const styles = StyleSheet.create({
backgroundContainer: {
flex: 1, backgroundColor: 'green'
},
mainContainer: {
flex: 1, margin: 25,
borderWidth: 1, borderRadius: 5, borderColor: 'gray',
backgroundColor: 'white',
justifyContent: 'space-between'
},
formContainer: {
flex: 1, paddingLeft: 25, paddingRight: 25
},
centerContainer: {
alignItems: 'center', marginBottom: 10,
},
loginMsg: {
margin: 10,
fontSize: 20, color: 'gray'
},
inputFieldsContainer: {
flexDirection: 'row', margin: 10,
borderWidth: 1, borderRadius: 5, borderColor: 'gray',
},
inputFields: {
flexGrow: 1,
marginTop: 5, marginBottom: 5,
height: 30,
backgroundColor: 'rgba(255,255,255,0.4)',
paddingHorizontal: 10,
},
buttonContainer: {
flexDirection: 'row',
justifyContent: 'space-between'
},
buttonLogin: {
alignItems: 'center', height: 40, marginTop: 10, marginLeft: 50, marginRight: 50, padding: 5,
backgroundColor: '#2980b9',
borderWidth: 1, borderRadius: 5, borderColor: 'gray'
},
buttonRegister: {
alignItems: 'center', height: 40,
marginLeft: 50, marginRight: 50
},
buttonRegisterText: {
fontSize: 20, fontWeight: 'bold', color: 'gray'
},
loading: {
position: 'absolute',
left: 0, right: 0, top: 0, bottom: 0,
alignItems: 'center', justifyContent: 'center'
}, });
I used KeyboardAvoidingView, it also doesn't work.
I found this solution, You can take the base code.
Installation
npm i react-native-keyboard-aware-scroll-view --save
Usage
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view'
<KeyboardAwareScrollView>
<View>
<TextInput />
</View>
</KeyboardAwareScrollView>
You can find it here
The way it worked for me was to use position as behavior and -70 as vertical offset. Any non negative integer was creating a huge unused space when the keyboard pops up.
<KeyboardAvoidingView
style={styles.container}
behavior={Platform.OS === 'ios' ? 'position' : 'height'}
keyboardVerticalOffset={Platform.OS === 'ios' ? -70 : 70}
enabled>
<ScrollView>
...
</ScrollView>
</KeyboardAvoidingView>
<KeyboardAvoidingView
style={styles.mainContainer}
behavior="padding"
enabled
>
<View>.......................</View>
</KeyboardAvoidingView>
'Enabled or disabled KeyboardAvoidingView' should be add.
Keep in mind is always your top Parent with flex:1 then the child is then you text input container
<KeyboardAvoidingView style={{ flex: 1 }}
behavior={Platform.OS === "ios" ? "position" : null} enabled>
<ScrollView>
<View>
<View >
<Text maxFontSizeMultiplier={1.5} >
Sign in to your account{" "}
</Text>
<View
behavior="padding"
enabled
>
<TextInput
placeholder="Email address"
placeholderTextColor={Colors.grey}
style={styles.textInput}
onChangeText={(e) => setEmail(e.trim())}
autoCapitalize="none"
returnKeyType={"done"}
/>
</View>
</View>
</ScrollView>
</KeyboardAvoidingView>
// 2nd Solutions use this package
npm i react-native-keyboard-aware-scroll-view --save
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view'
<KeyboardAwareScrollView>
<View>
<TextInput />
</View>
</KeyboardAwareScrollView>

React Native Undefined is not an object (evaluating 'this.camera.capture')

export class CameraView extends Component {
render(){
return(
<View style={{
flex: 1,
width: '100%',
height: '100%',
}}>
<Camera
ref={(cam) => {
this.camera = cam;
}}
style={styles.preview}
aspect={Camera.constants.Aspect.fill}>
<CaptureControls></CaptureControls>
<Image></Image>
</Camera>
</View>
);
}
}
export class CaptureControls extends Component {
render(){
return(
<View style={{
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
bottom: 30,
}}>
<Image style={{
width: 30,
height: 30,
marginRight: 75,
}} source={require('img/opengallery.png')}></Image>
<TouchableHighlight onPress={this.takePicture.bind(this)}>
<Image style={{
width: 70,
height: 70,
}} source={require('img/takepicture.png')}> </Image>
</TouchableHighlight>
<Image style={{
width: 30,
height: 30,
marginLeft: 75,
}} source={require('img/switchcam.png')}></Image>
</View>
);
}
takePicture() {
this.camera.capture()
.then((data) => console.log(data))
.catch(err => console.error(err));
}
}
Here's my code. When pressing the takepicture button I get undefined is not an object. What is wrong with my code? takePicture() worked on a Text element though. I added a TouchableHighlight around the Image and added the onPress to TouchableHighlight, but now it crashes.
Kind regards,
Matthew
I guess you figured this out in the meantime. The 'camera' variable is assigned in the context of CameraView but the code tries to access it from within CaptureControls.

Stack two buttons like in a horizontal UIStackView in React Native

I have a login form with two text inputs stacked vertically and a container view with two buttons below the inputs:
I want the two buttons to stretch to fill the width of the button container (red), so each button would take half its size. However I can't get it to work. I've tried various combinations of flex* properties with no luck.
In native iOS I would use a UIStackView for this with orientation = horizontal. React Native documentation says that pretty much any layout can be achieved with flexbox.
This is what my component looks like right now:
import React, {Component} from 'react';
import {KeyboardAvoidingView, TextInput, View, StyleSheet} from 'react-native';
import Button from 'react-native-button';
export default class Login extends Component {
render() {
return (
<KeyboardAvoidingView style={styles.container}>
<TextInput
placeholder="LOGIN"
placeholderTextColor="#505050"
style={styles.input}/>
<TextInput
placeholder="PASSWORD"
placeholderTextColor="#505050"
style={styles.input}/>
<View style={styles.buttonContainer}>
<Button
onPress={() => this.logIn()}
style={[styles.button, styles.loginButton]}>
Log in
</Button>
<Button
onPress={() => this.register()}
style={[styles.button, styles.registerButton]}>
Register
</Button>
</View>
</KeyboardAvoidingView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingHorizontal: 16,
backgroundColor: 'lightgray'
},
input: {
height: 40,
marginBottom: 12,
paddingHorizontal: 8,
backgroundColor: 'white'
},
buttonContainer: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'red'
},
button: {
padding: 8,
color: 'white',
backgroundColor: 'steelblue'
},
loginButton: {
marginRight: 8
}
});
If I add flex: 1 to the button style they become:
What am I doing wrong?
Yes. Its because of the react-native-button component. You have to use the containerStyle property of the Button component to style the container.
import React, {Component} from 'react';
import {KeyboardAvoidingView, TextInput, View, StyleSheet} from 'react-native';
import Button from 'react-native-button';
export default class Login extends Component {
render() {
return (
<KeyboardAvoidingView style={styles.container}>
<TextInput
placeholder="LOGIN"
placeholderTextColor="#505050"
style={styles.input}/>
<TextInput
placeholder="PASSWORD"
placeholderTextColor="#505050"
style={styles.input}/>
<View style={styles.buttonContainer}>
<Button
onPress={() => this.logIn()}
style={styles.buttonText}
containerStyle={styles.button}
>
Log in
</Button>
<Button
onPress={() => this.register()}
style={styles.buttonText}
containerStyle={styles.button}
>
Register
</Button>
</View>
</KeyboardAvoidingView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingHorizontal: 16,
backgroundColor: 'lightgray'
},
input: {
height: 40,
marginBottom: 12,
paddingHorizontal: 8,
backgroundColor: 'white'
},
buttonContainer: {
height: 60,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'red'
},
button: {
flex: 2,
padding: 8,
backgroundColor: 'steelblue',
alignSelf: 'stretch',
justifyContent: 'center',
},
buttonText: {
color: 'white',
}
});

React Native showing blank screen

I'm using react-native latest release 0.4.4 and installed react-native-side-menu and react-native-carousel components.
For some reason when I run the app, it's not showing anything.
The code instantiate a view with sidemenu and contentview inside.
contentview is for displaying the main page which contains three buttons and when someone clicks on explore it takes them to search page using navigatorios component.
Is there is some problem with StyleSheet ?
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
Component,
NavigatorIOS,
View,
TouchableHighlight,
TextInput,
} = React;
var SideMenu = require('react-native-side-menu');
var Menu = React.createClass({
about: function() {
this.props.menuActions.close();
},
render: function() {
return (
<View>
<Text>Menu</Text>
<Text onPress={this.about}>About</Text>
</View>
);
}
});
var styles = StyleSheet.create({
searchView: {
flexDirection: 'row',
padding: 25,
marginTop: 100,
},
text: {
color: 'black',
backgroundColor: 'white',
fontSize: 30,
margin: 80
},
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#F5FCFF',
},
flowRight: {
flexDirection: 'column',
padding: 25,
alignItems: 'center',
alignSelf: 'stretch',
marginTop: 100,
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
button: {
height: 36,
flex: 1,
flexDirection: 'row',
backgroundColor: '#009D6E',
borderColor: '#008C5D',
borderWidth: 1,
borderRadius: 8,
marginBottom: 10,
alignSelf: 'stretch',
justifyContent: 'center'
},
buttonText: {
fontSize: 18,
color: 'white',
alignSelf: 'center'
},
});
class Search extends Component {
render() {
return (
<View style={styles.searchView}>
<TextInput
style={{ flex: 2, height: 40, borderColor: 'gray', borderWidth: 1, padding: 5, borderRadius: 5}}
onChangeText={(text) => this.setState({input: text})}
placeholder="Search Appointment" />
<TextInput
style={{ flex: 1, height: 40, borderColor: 'gray', borderWidth: 1, padding: 5, borderRadius: 5}}
onChangeText={(text) => this.setState({input: text})}
placeholder="Location" />
</View>
);
}
}
class Welcome extends React.Component {
onExplorePressed() {
this.props.navigator.push({
title: 'Explore',
component: Search,
});
}
render() {
return (
<View style={styles.flowRight}>
<Text style={styles.welcome}>
Welcome to Docit!
</Text>
<TouchableHighlight style={styles.button} underlayColor='#009D3E'>
<Text style={styles.buttonText}>Sign In with Facebook</Text>
</TouchableHighlight>
<TouchableHighlight style={styles.button} underlayColor='#009D3E'>
<Text style={styles.buttonText}>Sign In with Email</Text>
</TouchableHighlight>
<Text style={styles.welcome}>OR</Text>
<TouchableHighlight
onPress={this.onExplorePressed.bind(this)}
style={styles.button}
underlayColor='#009D3E'>
<Text style={styles.buttonText}>Explore Something</Text>
</TouchableHighlight>
</View>
);
}
}
class ContentView extends Component {
render() {
return (
<NavigatorIOS
style={styles.container}
initialRoute={{
component: Welcome,
title: 'Welcome',
}}/>
)
}
}
var Docit = React.createClass({
render: function() {
var menu = <Menu navigator={navigator}/>;
return (
<SideMenu menu={menu}>
<ContentView/>
</SideMenu>
);
}
});
AppRegistry.registerComponent('Something', () => Something);
Am I doing something wrong here ??
I figured out that react-native-carousel component was conflicting with sidebar menu component and showing a blank screen. I posted the issue on their repository.
The simple solution is remove react-native-carousel component from everywhere, even from node_modules/, the folder inside.
Your react versions could be out of sync. I've had this happen to me when the react version bundled with the iOS app is different than the version that the packager is running.
If you're using cocoapods, make sure that the react version in your podfile is the same as your package.json.

Resources