Home > OS >  Can someone please help me implement Font.loadAsync
Can someone please help me implement Font.loadAsync

Time:06-24

I have used Font.loadAsync many times but for some reason, I keep getting errors while trying to use it this time. (code is at the pastebin, it got formatted weirdly on here)

pastebin

import React from "react";

CodePudding user response:

I was also struggling with Font.loadAsync in my latest project. I decided to use the expo-font module instead. You can initialize this module in App.js and it will load the fonts for all of your screens.

Here is an example:

import React from "react";
...

import { useFonts } from "expo-font"; //need to load fonts asynchronously

...

const App = () => {

  const [loaded] = useFonts({// function for expo-font 
    RobotoBlack: FONTS.Roboto-Black.ttf,
    RobotoBold: FONTS.Roboto-Bold.ttf,
    RobotoRegular:FONTS.RobotoRegular.ttf,
  });
  if (!loaded) {
    return null;
  }

...

CodePudding user response:

Try implementing require before your fonts. May need to hunt down where your fonts are coming from in your directory.


const App = () => {

  const [loaded] = useFonts({// function for expo-font 
    RobotoBlack: require(*font source directory*),
    RobotoBold: require(*font source directory*),
    RobotoRegular: require(*font source directory*)
  });
  if (!loaded) {
    return null;
  }

Source: https://docs.expo.dev/versions/latest/sdk/font/

  • Related