简体   繁体   中英

How to add in NativeBase footer in my react native app?

I am newbie in React Native. I would like to add in footer bar in my App. Here is my full code in dashboard.js file but it does not work:-

import React, { memo } from 'react';
import Background from '../components/Background';
import Logo from '../components/Logo';
import Header from '../components/Header';
import Paragraph from '../components/Paragraph';
import Button from '../components/Button';

import {Container, Footer, Title, Icon} from 'native-base';


const Dashboard = ({ navigation }) => (
  <Background>
    <Logo />
    <Header>Let’s start</Header>
    <Paragraph>
      Your amazing app starts here. Open you favourite code editor and start
      editing this project.
    </Paragraph>
    <Button mode="outlined" onPress={() => navigation.navigate('HomeScreen')}>
      Logout
    </Button>

    **<Container>
                <Footer>
                    <Button transparent>
                        <Icon size={30} color={'#fff'} name={'ios-telephone'} />
                    </Button>
                    <Title>Footer</Title>
                    <Button transparent >
                        <Icon size={25} color={'#fff'} name={'chatbox'}/>
                    </Button>
                </Footer>
            </Container>**
  </Background>

);

export default memo(Dashboard);

The error show that the fontfamily issue caused in native-base.

在此处输入图片说明

Therefore, I did some research and getting this solution.

async componentDidMount() {
    await Font.loadAsync({
      Roboto: require('native-base/Fonts/Roboto.ttf'),
      Roboto_medium: require('native-base/Fonts/Roboto_medium.ttf'),
      ...Ionicons.font,
    });
    this.setState({ isReady: true });
  }

But I have no idea how to add this in my code, anyone can help for this? Appreciated.

First you need to keep an initial state as false until data is loaded

state = {
    isReady: false
  }

Then inside componentDidMount you can load data & when the data is loaded change state.

async componentDidMount() {
    try {
      await Font.loadAsync({
        Roboto: require('native-base/Fonts/Roboto.ttf'),
        Roboto_medium: require('native-base/Fonts/Roboto_medium.ttf'),
        ...Ionicons.font,
      });
      this.setState({ isReady: true })
    } catch (error) {
      console.log('error loading fonts', error);
    }
  }

Finally render your data

render() {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        {
          this.state.isReady ?
            <Text style={{ fontFamily: 'Roboto', fontSize: 56 }}>Hello, world!</Text>
            :
            <View style={{ flex: 1, padding: 20 }}>
              <ActivityIndicator />
            </View>
        }
      </View>
    )
  }

You can apply the same scenario for functional component, But use useEffect() instead of componentDidMount()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM