Loading a custom font on React Native Expo gives an error - reactjs

So I am trying to create an app with React Native Expo CLI. I have installed the blank version. I am trying to import the Open Sans typeface into my application. I have done the following steps so far:
Made a folder called "fonts" inside of my "assets" folder, placed the ttf file for Open Sans font inside of it.
Added the following code in my App.js file
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import * as Font from 'expo-font';
export default class App extends React.Component {
constructor() {
super();
this.state = {
fontLoaded: false
};
}
async componentDidMount() {
try {
await Font.loadAsync({
'OpenSans': require('./assets/fonts/OpenSans.ttf')
});
this.setState({ fontLoaded: true });
}
catch( error ) {
console.log(error)
}
}
render() {
return(
<View>
<Text style={styles.text}>Test</Text>
</View>
);
}
}
const styles = StyleSheet.create({
text: {
fontFamily: 'OpenSans',
}
});
Unfortunately, I'm getting an error and all the solutions that I saw on the internet didn't help me. The error which I get is the following:
fontFamily "OpenSans" is not a system font and has not been loaded through Font.loadAsync.
- If you intended to use a system font, make sure you typed the name correctly and that it is supported by your device operating system.
- If this is a custom font, be sure to load it with Font.loadAsync.
- node_modules\react-native\Libraries\YellowBox\YellowBox.js:59:8 in error
- node_modules\expo\build\environment\muteWarnings.fx.js:27:24 in error
- ... 24 more stack frames from framework internals

You are loading the font after the app is mounted, that why you have that error.
your render method should look something like this:
render() {
if (!this.state.fontLoaded) {
return (<View>{/*some loader*/}</View>);
}
return(
<View>
<Text style={styles.text}>Test</Text>
</View>
);
}

Related

How can i get my ReactJS project to display Unity 3D Game? I've tried react unity WEBGL

I just finished coding a website with a Virtual Tour made on Unity, i've exported it as WEBGL with compression format disabled, and decompression fallback off(Unity). Then added this to my code on React
import React from "react";
import Unity, { UnityContext } from "react-unity-webgl";
const unityContext = new UnityContext({
loaderUrl: "../Build/unity341.loader.js",
dataUrl: "../Build/unity341.data",
frameworkUrl: "../Build/unity.framework.js",
codeUrl: "../Build/unity341.wasm",
});
function Game() {
return <Unity unityContext={unityContext} />;
}
export default Game;
then imported the game on the page
import React, {Component} from 'react';
import Game from './game.jsx';
import {Container} from 'react-bootstrap';
class GamePage extends Component {
render() {
return (
<div>
<Game/>
<Container>
//text and stuff inside the container
</Container>
</div>
);
}
}
export default GamePage;
But when i render it on my localhost i dont get the Unity viewer, and when inspecting on Chrome the only thing i see where the game should be is the following tag
<canvas class>
Thanks for your help guys !
Put generated WebGL files from Build folder to public folder in react. See picture. Those files must be in public folder because react server is hosting static files in this folder. For example i have put them to public\build folder. See picture.
Code in react:
const unityContext = new UnityContext({
loaderUrl: 'build/<AppName>.loader.js',
dataUrl: 'build/<AppName>.data',
frameworkUrl: 'build/<AppName>.framework.js',
codeUrl: 'build/<AppName>.wasm',
});
<Unity unityContext={unityContext} style={{ width: '100%', height: '100%' }} />

The AppNavigator is not exporting the required page

My SplashScreen page is not being exported by the AppNavigator even though the variable is declared and read, on the SplashScreen page.
I have tried to use createDrawerNavigator but I get the same result. I have tried to reset the cache with:
rm -rf node_modules && npm install
npm start -- --reset-cache
I have tried to reInstall the createDrawerNavigator
The error is below:
Unable to resolve module `./navigation/AppNavigator` from
`/Users/jaseyacey/Desktop/beermeapp4/screens/Splash.Screen.js`:
The module `./navigation/AppNavigator` could not be found from
`/Users/jaseyacey/Desktop/beermeapp4/screens/Splash.Screen.js`.
Indeed, none of these files exist:
The SplashScreen code is below:
import React, {Component } from 'react';
import {Image, View } from 'react-native';
import { inject } from 'mobx-react';
import AppNavigator from './navigation/AppNavigator';
import { createDrawerNavigator } from 'react-navigation-drawer';
#inject("stores")
class SplashScreen extends Component {
constructor(props) {
super(props)
}
componentDidMount() {
const {stores, navigation } = this.props;
setTimeout(() => {
navigation.navigate("Login")
}, config.store.SplashTime )
}
render() {
const { stores } = this.props
return (
<View style={{flex: 1}}>
<image style={{flex: 1, width: null, height: null}} source= {config.store.SplashImg}/>
</View>
)
}
}
export default AppNavigator;
You should add you Splash Screen into your App Navigator and there you will create an App Container with your navigation type (drawer navigation in this case) and export the App Navigator containing your splash Screen as your first screen of navigation.
I don't have a Splash Screen on my project yet, but you can see how I configured my App Navigation here: https://github.com/pahferreira/destetik-mobile/blob/master/src/navigation/AppNavigation.js
Or you can check here on their documentation: https://reactnavigation.org/docs/en/hello-react-navigation.html

How to get files uploaded on FTP server with React native?

Actually, I want to get the files with directory structure uploaded on FTP server using React-native.
I have tried to find libraries but I didn't find anything related to it.
So can you please help me with this problem?
import React, { Component } from 'react';
import { StyleSheet, View, Button } from 'react-native';
import FTP from 'react-native-ftp';
export default class App extends Component {
onButtonpress() {
FTP.setup("IP", 21) //Setup host
FTP.login("username", "password").then(
(result) => {
FTP.list(".").then(
(result) => {
console.log(result);
}
);
},
(error) => {
alert(error);
}
)
}
render() {
return (
<View >
<Button onPress={this.onButtonpress.bind(this)} title="hi" />
</View>
);
}
}
Here is the sample i have tried but i got error while impoting 'react-native-ftp'.
I got error "Could not find declaration for module 'react-native-ftp'".
The error you encounter is because of react-native-ftp functions are not available on ios - they are not implemented. You need to fork the repo and implement them to work with ios too.
and then:
You can download a file like below using react-native-ftp:
FTP.downloadFile("./nameOfFileToBeDownloaded","localPathWhereItWillBeSaved")
.then(result=>console.log(result))
.catch(error=>alert(error))
Or upload with uploadFile:
FTP.uploadFile("./nameOfFileToBeUploaded","remotePathWhereItWillBeSaved")
.then(result=>console.log(result))
.catch(error=>alert(error))
If you want entire folder, you can zip it first with react-native-zip-archive and the upload just the zip.

Invariant Violation: View config not found for name option (React Native)

I'm trying to use this library https://github.com/country-regions/react-country-region-selector for my react native application.
The example code is as follows:
import React from "react";
import { View, Text } from 'react-native';
// note that you can also export the source data via CountryRegionData. It's in a deliberately concise format to
// keep file size down
import {
CountryDropdown,
RegionDropdown,
CountryRegionData
} from "react-country-region-selector";
class Example extends React.Component {
constructor(props) {
super(props);
this.state = { country: "", region: "" };
}
selectCountry(val) {
this.setState({ country: val });
}
selectRegion(val) {
this.setState({ region: val });
}
render() {
const { country, region } = this.state;
return (
<View>
<CountryDropdown
value={country}
onChange={val => this.selectCountry(val)}
/>
<RegionDropdown
country={country}
value={region}
onChange={val => this.selectRegion(val)}
/>
</View>
);
}
}
export default Example;
I changed the divs in the render method into View, which has left me with the current error: Invariant Violation: View config not found for name option.
I'm not sure if this is because the library is intended for React as opposed to React-Native or if there is something else going on that I'm unaware of.
This won't work because this library renders HTML, which is not available in react-native. You can confirm this by going to node_modules/react-country-region-selector/src to see the source code.
However, the Picker component in react-native has a very similar API, so you could easily remake this to be compatible. Note that you should not edit files in your node_modules as they will be corrected any time you run yarn / npm. Instead, you should create your own local version of this module.
It's really just a matter of replacing select with Picker and option with Picker.Item and changing the onChange handlers to work with the Picker instead of expecting a DOM event.
You can learn more about the Picker API here

module appregistry is not a registered callable module (calling runApplication)

i cannot find a way to make react-navigation work at all. i copied the working examples from the internet but they don't seem to work too. can someone tell me what i am doing wrong.
i'm using
node: 8.9.4
react: 16.3.0-alpha.1
react-native: 0.54.0
react-navigation: ^1.4.0
//index.js
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
import {
TabNavigator,
StackNavigator
} from 'react-navigation';
import Home from './first';
import Homes from './second';
export default class demoApp extends Component {
render() {
return (
<SimpleNavigation/>
);
}
}
export const SimpleNavigation = StackNavigator({
Home: {
screen: Home,
header: { visible: false },
navigationOptions: {
title: 'Home',
header: null
},
},
Homes: {
screen: Homes,
navigationOptions: {
title: 'second'
},
},
},{});
Here's the first tab
//first.js
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image,
TextInput,
Button,
TouchableHighlight
} from 'react-native';
export default class Home extends Component {
constructor(props){
super(props);
this.state = {zipCode: ''}
}
navigate = (zipCode) => {
this.props.navigation.navigate('Search', zipCode);
}
render() {
return (
<View>
<View>
<Text>An application to do things</Text>
<TextInput
placeholder='Enter a Zip Code'
onChangeText={(zipCode) => this.setState({zipCode})}
>
</TextInput>
</View>
<View>
<TouchableHighlight onPress={() => this.navigate(this.state.zipCode)}>
<Text>
Search
</Text>
</TouchableHighlight>
</View>
</View>
);
}
}
i cant seem to make it run at all. I tried following many other tutorials as well. But none of them worked. What am i doing wrong?
I kept getting this error too today, and it was very annoying. Was able to get rid of it by removing the Terminal window with "Metro" bundler stuff, and then recompiling the app.
Seems like it's not the code, but rather the runtime environment (which seems to work well with only one app example at a time). You can double check this by doing a super simple app that should work.
Close your Nodejs Terminal and run again
Just kill all node process and start npm server and run application:
Step1: run command killall -9 node
Step2: run command npm start
Step3: run command react-native run-ios OR react-native run-android

Resources