Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't link assets (fonts) in react native >= 0.60

Tags:

react-native

react-native: 0.60.4,
react: 16.8.6,
npm: 6.10.1
XCode: 10.2.1
AndroidStudio: 3.4.1

I created a project using

npx react-native init awesomeApp --template typescript

I have put my assets in the

assets/fonts/<Bunch of .ttf files>

My Directory Structure

awesomeApp
|
+--android
+--ios
+--assets
|  |
|  +---fonts
|      |
|      +-- ProximaNova-Bold.ttf
+--react-native-config.js

then I ran react-native link

Nothing happends, XCode not showing any added Resource neither the android, and when I'm running react-native run-ios showing error that the font is not found.

My react-native-config.js

module.exports = {
    project: {
        ios: {},
        android: {},
    },
    assets: ['./assets/fonts']
};

I have also tried yarn react-native link and npx react-native link

like image 264
Siraj Alam Avatar asked Jul 21 '19 04:07

Siraj Alam


People also ask

Why react-native link is not working?

If you did not use the react-native link command but still received the error, it's likely that one of your dependencies is not suitable for autolinking. If that is the case, you can try using [patch-package](https://www.npmjs.com/package/patch-package) to fix it on your own.

How to add custom fonts in React Native?

Custom fonts can be added into React Native project (0.60+) with the following steps. 1. Get the font files needed for the project The font files should be either in .ttf or .otf format. The font files can be obtained from Google fonts. Create a folder named fonts under the assets directory and place the font files in it.

Is React-Native link ./assets/fonts valid?

So react-native link ./assets/fonts is not a valid command, but it should work. Do remember you need your fonts dir specified in package.json as per Hamed's answer. This error has no effect on linking assets.

How do I link my fonts to react-native?

So if you ever just want to link your fonts you can do that by linking some specific library along with it. If you just want to link your assets folder, try react-native link "package-that-does-not-exist", it will still link your assets without linking anything else.

How to link only the React-Native-assets folder?

If you just want to link your assets folder, try react-native link "package-that-does-not-exist", it will still link your assets without linking anything else. Show activity on this post. There is a suitable package called react-native-assets, which links & unlinks only the assets.


2 Answers

Well, finally, I linked my fonts,

I just renamed my file react-native-config.js to react-native.config.js and it worked.

I don't know if this is the right way, I just tried and it worked, for me at least.

Here is the complete walkthrough for linking custom fonts.

like image 169
Siraj Alam Avatar answered Sep 23 '22 06:09

Siraj Alam


my react-native.config.js

module.exports = {
project: {
    ios: {},
    android: {},
},
assets: ['./assets/fonts/'],
};

and logs:

info Linking assets to ios project                                                                                      
info Linking assets to android project                                                                                  
success Assets have been successfully linked to your project

but My mistake was:

Wrong way:

fontFamily: BYekan

Right way:

fontFamily: 'BYekan'

just put font name in ''

like image 41
Mohammad vatandoost Avatar answered Sep 22 '22 06:09

Mohammad vatandoost