Search code examples
reactjsreact-nativenative-base

Loading font native-base error


I'm getting the error: You started loading 'Roboto_medium', but used it before it finished loading when using native base.

enter image description here

I've followed the instructions in the official page.

To create react native app I'm using create-react-native-app.

App.js

export default class App extends React.Component {

async componentWillMount() {
  await Expo.Font.loadAsync({
  'Roboto': require('native-base/Fonts/Roboto.ttf'),
  'Roboto_medium': require('native-base/Fonts/Roboto_medium.ttf'),
  'Ionicons': require('@expo/vector-icons/fonts/Ionicons.ttf'),
 });
}

 render() {
   return (
    <Container>
      <StatusBar hidden={true} />

    <Button>
      <Text>
        Button
      </Text>
    </Button>

    <ListaItens />
    </Container>
  );
}
} 

Solution

  • you need to wait till the fonts get loaded. You can do something like this

    import React from "react";
    import { StatusBar } from "react-native";
    import { Container, Button, text, ListItem, Text } from "native-base";
    import Expo from "expo";
    
    export default class App extends React.Component {
      constructor(props) {
        super(props);
        this.state = { loading: true };
      }
    
      async componentWillMount() {
        await Expo.Font.loadAsync({
          Roboto: require("native-base/Fonts/Roboto.ttf"),
          Roboto_medium: require("native-base/Fonts/Roboto_medium.ttf"),
          Ionicons: require("@expo/vector-icons/fonts/Ionicons.ttf"),
        });
        this.setState({ loading: false });
      }
    
      render() {
        if (this.state.loading) {
          return <Expo.AppLoading />;
        }
        return (
          <Container>
            <StatusBar hidden={true} />
    
            <Button>
              <Text>Button</Text>
            </Button>
    
            <ListItem />
          </Container>
        );
      }
    }