I use react-native-view-pdf, React Native version is 0.59.5
https://github.com/rumax/react-native-PDFView
I just follow the tutorial but show blank screen.
I can't figure it out. I don't know why they are showing empty screens.
Step1:
npm install react-native-view-pdf --save
Step2:
react-native link react-native-view-pdf
Use the code and type react-native run-ios
App.js:
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
import PDFView from 'react-native-view-pdf/lib/index';
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
android:
'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
const resources = {
file: Platform.OS === 'ios' ? 'downloadedDocument.pdf' : '/sdcard/Download/downloadedDocument.pdf',
url: 'https://www.ets.org/Media/Tests/TOEFL/pdf/SampleQuestions.pdf',
base64: 'JVBERi0xLjMKJcfs...',
};
type Props = {};
export default class App extends Component<Props> {
render() {
const resourceType = 'url';
return (
<View style={styles.container}>
<Text style={styles.welcome}>Welcome to React Native!</Text>
<PDFView
fadeInDuration={250.0}
style={{ flex: 1 }}
resource={'https://www.ets.org/Media/Tests/TOEFL/pdf/SampleQuestions.pdf'}
resourceType={resourceType}
onLoad={(event) => console.log(`PDF rendered from ${event}`)}
onError={(error) => console.log('Cannot render PDF', error)}
/>
<Text>Bottom text</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,
},
});
Show black screen:
You can try below library it will help you to achieve and this is high ranked library
see here
Installation
npm library
npm install rn-fetch-blob --save
npm install react-native-pdf --save
react-native link rn-fetch-blob
react-native link react-native-pdf
Example
import React from 'react';
import { StyleSheet, Dimensions, View } from 'react-native';
import Pdf from 'react-native-pdf';
export default class PDFExample extends React.Component {
render() {
const source = {uri:'http://samples.leanpub.com/thereactnativebook-sample.pdf',cache:true};
//const source = require('./test.pdf'); // ios only
//const source = {uri:'bundle-assets://test.pdf'};
//const source = {uri:'file:///sdcard/test.pdf'};
//const source = {uri:"data:application/pdf;base64,..."};
return (
<View style={styles.container}>
<Pdf
source={source}
onLoadComplete={(numberOfPages,filePath)=>{
console.log(`number of pages: ${numberOfPages}`);
}}
onPageChanged={(page,numberOfPages)=>{
console.log(`current page: ${page}`);
}}
onError={(error)=>{
console.log(error);
}}
style={styles.pdf}/>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'flex-start',
alignItems: 'center',
marginTop: 25,
},
pdf: {
flex:1,
width:Dimensions.get('window').width,
}
});
I've recently used this library and it is very good as expected.
Try this:
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
import PDFView from 'react-native-view-pdf/lib/index';
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
android:
'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
const resources = {
file: Platform.OS === 'ios' ? 'downloadedDocument.pdf' : '/sdcard/Download/downloadedDocument.pdf',
url: 'https://www.ets.org/Media/Tests/TOEFL/pdf/SampleQuestions.pdf',
base64: 'JVBERi0xLjMKJcfs...',
};
type Props = {};
export default class App extends Component<Props> {
render() {
const resourceType = 'url';
return (
<View style={{flex: 1, witdth: '100%'}>
<Text style={styles.welcome}>Welcome to React Native!</Text>
<PDFView
fadeInDuration={250.0}
style={{ flex: 1 }}
resource={'https://www.ets.org/Media/Tests/TOEFL/pdf/SampleQuestions.pdf'}
resourceType={resourceType}
onLoad={(event) => console.log(`PDF rendered from ${event}`)}
onError={(error) => console.log('Cannot render PDF', error)}
/>
<Text>Bottom text</Text>
</View>
);
Related
I simply created a react-native-project using expo and added react-native-tts and tried to use the speak function but it is giving Native module cannot be null.
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { Button, StyleSheet, Text, View } from 'react-native';
import Tts from 'react-native-tts';
export default function App() {
return (
<View style={styles.container}>
<Button
onPress = {() => {Tts.speak("Hello, world!")}}
title = "voice"
/>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
This is an expo project and react-native-tts is not made for expo. So for text to speech I need to use expo-speech or eject the project from expo.
Looking to have button enter go into another screen:
need help with navigation screen.
I keep getting error:
Cannot read property 'navigation' of undefined
Evaluating App.js
Loading App.js
TypeError: Cannot read property 'navigation' of undefined
https://snack.expo.io/#ganiyat1/colorful-thrills
import * as React from 'react';
import { Text, View, StyleSheet, ImageBackground, Image, Button } from 'react-native';
import Constants from 'expo-constants';
import { StackNavigator} from 'react-navigation';
import Books from './components/Books';
// You can import from local files
// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';
const Book = StackNavigator({
Books: { screen: Books },
});
const { navigate } = this.props.navigation;
export default function App() {
return (
<View style={styles.container}>
<View style={styles.topContainer}>
<Text style={styles.title}> Colorful Thrills
</Text >
</View>
<View style={styles.bottomContainer}></View>
<View style={styles.imageContainer}>
<Image
style={styles.image}
source={require('./assets/bookcover.png')}
/>
<Text style={styles.paragraph}>
{"\n"} BOOKWORMS, UNITE! {"\n"} {"\n"}
Suspense, Mystery and Thrillers by Authors of Color
</Text>
<Button
color='#ff914d'
title= 'ENTER'
onPress={() =>
navigate('Books')}
/>
</View>
</View>
);
}
In the above code snippet, I don't see a default Navigator being returned form the entry file, which is App.js by default in React Native.
I assume that you just started to learn React Native, so I will spare you all the minor details and walk you through the solution.
I refactored the App.js file to a into a new component file in /components/Home.js.
Added a default stack Navigator in App.js which has two screens, Home and Books.
Now you can access all the Navigation props in your Home and Books component, as it is being declared in the Navigator variable in App.js
Here is a live demo of your code on Expo.
//App.js
import * as React from 'react';
import { Text, View, StyleSheet, ImageBackground, Image, Button } from 'react-native';
import Constants from 'expo-constants';
import { StackNavigator} from 'react-navigation';
import Books from './components/Books';
import Home from './components/Home'
import { Card } from 'react-native-paper';
const Navigator = StackNavigator({
Books: { screen: Books },
Home:{screen:Home}
});
export default function App(props) {
return (
<Navigator />
);
}
//component/Books.js
import React, { useState } from 'react';
import { StyleSheet, SafeAreaView,Button } from 'react-native';
import MaterialTabs from 'react-native-material-tabs';
const Books = (props) => {
const {navigation} = props
const [selectedTab, setSelectedTab] = useState(0);
return (
<SafeAreaView style={styles.container}>
<MaterialTabs
items={['New Releases', 'All', 'BOM']}
selectedIndex={selectedTab}
onChange={setSelectedTab}
barColor="#1fbcd2"
indicatorColor="#ff914d"
activeTextColor="white"
/>
<Button
color='#ff914d'
title= 'Home'
onPress={() =>
navigation.navigate('Home')}
/>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
export default Books
//component/Home.js
import React from 'react'
import {View,Text,StyleSheet,Button,Image} from 'react-native'
const Home = (props) => {
const {navigation} = props
return (
<View style={styles.container}>
<View style={styles.topContainer}>
<Text style={styles.title}> Colorful Thrills
</Text >
</View>
<View style={styles.bottomContainer}></View>
<View style={styles.imageContainer}>
<Image
style={styles.image}
source={require('../assets/bookcover.png')}
/>
<Text style={styles.paragraph}>
{"\n"} BOOKWORMS, UNITE! {"\n"} {"\n"}
Suspense, Mystery and Thrillers by Authors of Color
</Text>
<Button
color='#ff914d'
title= 'ENTER'
onPress={() =>
navigation.navigate('Books')}
/>
</View>
</View>
)
}
export default Home
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
},
topContainer: {
flex: 1,
backgroundColor: '#ff914d',
},
bottomContainer: {
flex: 1,
backgroundColor: '#96d0e3',
},
imageContainer: {
position: 'absolute',
width: '100%',
height: '100%',
justifyContent: 'center',
alignItems: 'center',
},
image: {
width: 300,
},
title:{
margin: 24,
marginTop: 50,
fontSize: 40,
fontWeight: 'bold',
textAlign: 'center',
fontFamily: 'GillSans-Italic',
},
paragraph: {
margin: 24,
marginTop: 0,
fontSize: 20,
fontWeight: 'bold',
textAlign: 'center',
}
});
I am making react native expo app. I wanted to add FB ads to my app. I have done all like in the docs, but it gives me an error:
"errorCode": 1203,
"errorMessage": "The SDK version in the ad request is no longer supported for new apps. Please upgrade to one of the latest versions of the SDK",
},
How I can download the latest fd ads SDK? I downloaded SDK from this page:
https://github.com/expo/expo/tree/master/packages/expo-ads-facebook
I am using expo sdk 33. Please help me. I need it very much
Code:
// page.js
import React from 'react';
import { View } from 'react-native';
import { createStackNavigator, createAppContainer } from 'react-navigation'; // Version can be specified in package.json
import * as FacebookAds from 'expo-ads-facebook';
import AdScreenFacebook from './AdScreenFacebook'
const adsManager = new FacebookAds.NativeAdsManager("2272791379702600_2272795453035526", 10);
class AdScreen extends React.Component {
render () {
return (
<View>
<AdScreenFacebook adsManager={adsManager} />
<FacebookAds.BannerAd
placementId="2272791379702600_2272803043034767"
type="standard"
onPress={() => console.log('click')}
onError={error => console.log('error', error)}
/>
</View>
);
}
}
export default createStackNavigator(
{
Main: {
screen: AdScreen,
},
AdScreenFacebook: {
screen: AdScreenFacebook,
}
},
{
initialRouteName: 'Main',
}
);
// AdScreenFacebook.js
import React from 'react';
import { StyleSheet, Text, View, Dimensions } from 'react-native';
import * as FacebookAds from 'expo-ads-facebook';
const { AdTriggerView, AdMediaView, AdIconView } = FacebookAds;
class AdScreenFacebook extends React.Component {
render () {
return (
<View style={{ flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',}}>
<View style={{ width:500}}>
<AdMediaView style={{ width: 160, height: 90 }}/>
<View style={{flexDirection: 'row'}}>
<AdIconView style={{ width: 50, height: 50 }}/>
<AdTriggerView>
<Text>{this.props.nativeAd.bodyText}</Text>
<Text>{this.props.nativeAd.callToActionText}</Text>
</AdTriggerView>
</View>
</View>
</View>
);
}
}
export default FacebookAds.withNativeAd(AdScreenFacebook);
I have try to use textAlign: 'justify' property in react native.TextAlign justify doesn't work on Android.
React native version: "react-native": "0.59.5"
import React from 'react';
import { Image, View } from 'react-native';
import { Text, Button } from 'native-base';
import { connect } from 'react-redux';
import EStyleSheet from 'react-native-extended-stylesheet';
import { colors } from '../styles';
class Login extends React.Component {
render() {
return (
<View style={styles.container}>
<View style={styles.contentCenter}>
<Text style={[styles.titleContent,styles.fontBold]}>Simple Secure</Text>
<Text style={[styles.titleContent,styles.fontBold]}>Reliable Messaging</Text>
</View>
</View>
);
}
}
const styles = EStyleSheet.create({
titleContent: {
color:'#62778c',
fontSize: '1.4rem',
textAlign: 'justify',
}
});
const mapStateToProps = (state) => {
return {};
};
export default connect(mapStateToProps)(Login);
checkout the offical document:
textAlign: enum('auto', 'left', 'right', 'center', 'justify')
Specifies text alignment. The value 'justify' is only supported on iOS and fallbacks to left on Android.
As mentioned above android does not support text justification but you can go around this by adding the html text inside WebView like this:
<WebView
source={{ html: "<p style='text-align:justify;'>Your justified text</p>" }}
/>
When I am accessing const value of a js file from my another js file am getting undefined Data
Abc.js
import { Platform } from 'react-native';
const abc = Platform.select({
ios: [
'com.abc',
'com.xyz'
],
android: [
'com.pqr',
'com.lmn'
]
});
export default abc
App.js
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
Button,
View
} from 'react-native';
import { abc } from "./Abc";
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' +
'Cmd+D or shake for dev menu',
android: 'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
onPressgetData = () => {
console.warn("DATA " + JSON.stringify(abc));
}
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
!! Welcome to React Native !!
</Text>
<Button title="get Data"
color="#841584"
onPress={() => onPressgetData()} />
</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,
},
});
I am accessing abc const data from my App.js when I click get Data instead of array I am getting undefined in warning. I don't know where I have mistaken Thank you in advance
You've exported your module as default and importing it as a named import, therefore it is undefined.
Change
import { abc } from "./Abc";
to
import abc from "./Abc";
to get correct result
When you are exporting a single component from a file you can use export default abc and this can be imported directly in other files by import abc from './abc'
When your files have many components/functions, you will have to export each function individually using export functionName and import them using
import { functionName, functionName2, functionName3 } from './abc'
You can also use export default SpecialFunctionName along with other exports.
So, when you
import SpecialFunctionName(can use any name here) from './abc'
you will always get SpecialFunctionName as it was default export-ed.