如何在我的本机应用程序中添加NativeBase页脚?

问题描述 投票:0回答:1

我是React Native的新手。我想在我的应用程序中添加页脚栏。这是我在dashboard.js文件中的完整代码,但是它不起作用:-

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);

该错误表明fontfamily问题是在本机库中引起的。

enter image description here

因此,我进行了一些研究并获得了此解决方案。

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 });
  }

但是我不知道如何在我的代码中添加它,有人可以为此提供帮助吗?感激。

javascript android reactjs react-native native-base
1个回答
0
投票

首先,在加载数据之前,您需要将初始状态保持为false

state = {
    isReady: false
  }

然后在componentDidMount内部,您可以加载数据&加载数据时更改状态。

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);
    }
  }

最终呈现您的数据

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>
    )
  }

由于您正在使用功能组件,因此您可以应用相同的方案。但是请使用useEffect()而不是componentDidMount()

© www.soinside.com 2019 - 2024. All rights reserved.