How to display App loading in React native CLI (without expo) - react-native

I added custom font in my CLI app by configuring it in react-native.config.js
Now i want to display Apploading splash screen till my font and other required assets loaded on screen and app is ready to use.

Adding Fonts
If you are using react-native-cli you dont need to use AppLoading component to add your fonts, you just have to follow these simple steps for React Native Version > 0.60:
STEP 1: Add Fonts to Assets
Add the fonts you want to use to your project in src/assets/fonts for example.
STEP 2: Define assets directory (Which you already did i guess)
Create file react-native.config.js in your root project directory and add following code
module.exports = {
assets: ["./src/assets/fonts/"],
};
STEP 3: Link assets using react-native link
Run in terminal in your root project directory
$ npx react-native link
NOTE:
link command will link fonts in Info.plst for IOS, and creates fonts directory android/app/src/main/assets/fonts for Android, where copies of your custom fonts will be stored.
Splash screen
You can simply achieve that by configuring your root navigator e.g. StackNavigator to show a normal screen component that returns some sort of a loading indicator as the first screen to be rendered when your app first starts.
Then in that component, use useEffect to do some background tasks that need to be done before your app starts.
Finally, when the awaited tasks are done, simply navigate to your main app flow e.g. HomeScreen.js using the navigation.navigate() method.

Related

Why does react native only allows .png as app icon?

I have a expo managed react native project.
I have put an icon.jpg file in the assets folder and added the following line in app.json:
"icon": "./assets/icon.jpg",
Now when I run the project it shows the error:
Field: icon - field 'icon' should point to .png image but the file at './assets/icon.jpg' has type jpg.
I checked the expo documentation: https://docs.expo.dev/guides/app-icons/
It says that
Use png files.
What is the reason that expo only allows .png file for setting app icon and splash screen?

Showing SVG image with 'react-native-svg' library, however encounter 'Unrecognized font family' error in iOS (Android works fine.)

My react-native version is "0.62.2".
In order to show remote SVG images, I use react-native-svg library in my React Native project.
I use it like:
import {SvgUri} from 'react-native-svg';
const MyScreen = ({route, navigation}) => {
...
...
return (<View>
<SvgUri width="100%" height="100%" uri={imageSource} />
</View>)
}
export default MyScreen;
In Android emulator, it works fine!
In iOS, it works in a sense, but the way how it works is: when navigate to MyScreen, I always encounter the following error at runtime in the 1st place:
Then, I have to press on keyboard ctrl+S to save code again (though nothing needs to save) which triggers the simulator to refresh, then MyScreen is shown successfully with the SVG image.
Why I get the "Unrecognized font family 'Univers-Condensed'" error at runtime in iOS? How to get rid of it?
(In my code, I have no code using that font, so my guess is the library introduced that font.)
This happens when your app is trying to access the given font family and that specific font is not linked with your project and this error can happen only be on a single platform (Android, IOS) because these fonts linked separately on both platforms.
Solution:
Remove usage of Univers-Condensed font family in your JS code because JS code is the trigger point of this error.
Note: This solution will not work if this error is produced by some library that you are using in your project and that is using that Univers-Condensed font family.
Another solution is, You have to add and link this font family in your project.
Download the font .ttf file
Make a folder named assets and inside the assets, folder make the fonts
folder on the root of the project and put your downloaded font file in it.
add this to your react-native.config.js file
module.exports = {
assets: ['./assets/fonts/']
};
run react-native link
this process will automatically link fonts with your IOS and android project too.
Can you try this add Universe-Condensed.ttf in info.plist
<key>UIAppFonts</key>
<array>
<string>Universe-Condensed.ttf</string>
</array>

Importing custom icons into React Native 0.62

I'm trying to import and use my own custom icons in my React Native 0.62.2 app.
I followed the steps outlined here https://github.com/oblador/react-native-vector-icons#custom-fonts but so far the icons are not showing up.
Here are the steps I followed:
Created my icon set and converted it to fonts on https://icomoon.io
Downloaded the zip file from IcoMoon and placed the ttf file into ./src/assets/fonts folder
I then created react-native-config.js file and placed in the root. The code in this file is down below
Under my components folder, I created CustomIcon.js -- see code below
I also placed the selection.json file that was included in the zip file I downloaded from IcoMoon in the same folder as CustomIcon.js
I then used the CustomIcon as shown below
So here what the codes look like:
The react-native-config.js file looks like this:
module.exports = {
project: {
ios: {},
android: {},
},
assets: ['./src/assets/fonts/']
};
The CustomIcon.js file looks like this:
import { createIconSetFromIcoMoon } from 'react-native-vector-icons';
import icoMoonConfig from './selection.json';
export default createIconSetFromIcoMoon(icoMoonConfig, 'StreamLine', '../../../assets/fonts/streamline-icon-set-1.ttf');
And here's how I use the icon:
import CustomIcon from '../common_components/fonts/CustomIcon';
<CustomIcon name="home-outline" />
When I run the app in Android Emulator, I see the missing icon symbol i.e. a box with an X in it.
Any idea what the issue is here?
There is a really good article which helped me with this problem.
Custom icon fonts with React Native
There is always issue with custom icons. When I personally bump into such condition I do these:
Rename the react-native-config.js to react-native.config.js
Re-run the app by restarting metro
Make sure I have correct path to my assets in react-native.config.js
react-native link and restart. It copies your assets to corresponding ios and android folders
If neither do not help I copy the assets manually to folder: Project/android/app/src/main/assets/fonts

Why is react-native-svg-icons giving me the error that there are some duplicates for ttf files?

Long story short
I have sunk about 12 hours in my iPhone react-native app. Since yesterday it's not building anymore (out of nowhere). After 5 hours! of debugging I found what may caused the issue but not how to solve it.
I use these packages containing .ttf files / for icons and stuff:
"#fortawesome/fontawesome-svg-core"
"#fortawesome/free-solid-svg-icons"
"#fortawesome/react-native-fontawesome"
"#react-native-community/cli-platform-ios"
"react-native-vector-icons"
And I think there is the problem. Since react-native-vector-icons seems to use also the same .ttf file that one of these packages bring in I get the error:
Multiple Commands produce...
So when I uninstall react-native-vector-icons it works again. But then when I get to the screen where i use this package:
"react-native-elements" (which in the docs it says it needs react-native-vector-icons)
It's complaining that it can't find Fontawesome Icons (the Checkbox Component from elements)
So how can I fix this? I need the app to build obviously and also need the Checkbox Component from react-native-elements. But react-native-elements requires react-native-svg-icons. But when I install it it doesn't even build with the above error that there are multiple .ttf files of the same icon.
I could not figure it out but it has something to do with react-native-elements since they also link the icons that also come with react-native-vector-icons
I solved it by getting rid of react-native-elements and created my own custom Checkbox component.
I had a similar issue associated with the fonts included in the react-native-vector-icons library. I solved it with the following.
I created assets/fonts dir in my project. I added another some fonts that iOS didn't have linked ("Nunito" font in my case), but I didn't include fonts of react-native-vector-icons
I created a new file named react-native.config.js in the root of my project with the following:
module.exports = {
project: {
ios: {},
android: {}
},
assets: ['./src/assets/fonts/'],
};
Then I ran react-native link
Then I went to ios dir (cd ios) and ran pod update
Finally, I ran react-native run-ios and the magic happened
I bassed it on Alex Biddle's post: https://medium.com/better-programming/how-to-add-custom-fonts-to-your-react-native-project-c64305281b9

lack of files after generating APK

I am building an app , my app contains a folder called content that folder contains a menu.html file that is calling some js, css files
content (size 5 MG)
menu.html
css
js
images
my problem is that after having generated my apk , installed it on my mobile
it displays first screen , when i click to move to next screen (the screen that is calling menu.html) here it sucks , i get a simple empty screen !
my question should I import manually this content folder to my android assets folder ? should I remove the 2 existing files in assets index.android.bundle and index.android.bundle.meta ?
You could try linking it. Since rnpm is part of RN you don't need to install anything, just add an entry in your package.json
"rnpm": {
"assets": [
"./path/to/file.html"
]
}
and then run react-native link
If you're already using rnpm you can add another path separated by a comma to that assets array. I don't know for certain but I would imagine the react-native is going to by default ignore HTML files unless they are explicitly linked.
You could also manually store the file in your Android assets folder and require it from there, but this won't work for iOS so if you're going cross platform I would try to get it to work via link.
Also as a side note, WebView kind of sucks. For a better user experience I would suggest rewriting the contents of that file in a way that react-native can render them.