problem when implementing custom React source

Asked

Viewed 24 times

0

Speak devs, good afternoon. I’m having a problem implementing a custom font in my React-Native app.

The problem is this::

**fontFamily "open-sans" is not a system font and has not been loaded through Font.loadAsync**

Okay, all apparently I made an error in the "Font.loadSync" section, so I went to check it out, and just to make sure I got the correct paths from. ttf’s, copied them by windows explorer, but the problem persisted.

I searched some respesta in some post and the suggested install Exp-font by Node, I did more problem continues, and do not know how to solve.

Does anyone have any idea what’s going on?? In case anyone wants to take a look at the code, you will notice that it is a little game of "guessing a number":

import React, { useState } from 'react';
import { StyleSheet, View } from 'react-native';
import * as Font from 'expo-font';

import AppLoading from 'expo-app-loading'

import Header from './components/Header';
import StartGameScreen from './screens/StartGameScreen';
import GameScreen from './screens/GameScreen';
import GameOverScreen from './screens/GameOver';

const fetchFonts = () => {
  return Font.loadAsync({
    'open-sans': require('./assets/fonts/OpenSans-Regular.ttf'),
    'open-sans-bold': require('./assets/fonts/OpenSans-Bold.ttf')
  });
};

export default function App() {
  const [userNumber, setUserNumber] = useState();
  const [guessRounds, setGuessRounds] = useState(0);
  const [dataLoaded, setDataLoaded] = useState(false);

  if (!dataLoaded) {
    return (
      <AppLoading
        startAsync={fetchFonts}
        onFinish={() => setDataLoaded(true)}
        onError={console.warn}
      />
    );
  }

  const configureNewGameHandler = () => {
    setGuessRounds(0);
    setUserNumber(null);
    console.log(userNumber && guessRounds <= 0);
  };

  const startGameHandler = selectedNumber => {
    setUserNumber(selectedNumber);
  };

  const gameOverHandler = numOfRounds => {
    setGuessRounds(numOfRounds);
  };

  let content = <StartGameScreen onStartGame={startGameHandler} />;
 
  if (userNumber && guessRounds <= 0) {
    content = (
      <GameScreen userChoice={userNumber} onGameOver={gameOverHandler} />
    );
  } else if (guessRounds > 0) {
    
    content = (
      <GameOverScreen
        roundsNumber={guessRounds}
        userNumber={userNumber}
        onRestart={configureNewGameHandler}
      />
    );
  }

  return (
    <View style={styles.screen}>
      <Header title="Guess a Number" />
      {content}
    </View>
  );
}

const styles = StyleSheet.create({
  screen: {
    flex: 1
  }
});
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.