javascript - 如何在我的 React Native 应用程序中添加 NativeBase 页脚?

标签 javascript android reactjs react-native native-base

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

错误表明是native-base中的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 });
  }

但我不知道如何将其添加到我的代码中,任何人都可以帮忙吗?赞赏。

最佳答案

首先,您需要将初始状态保持为 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()

关于javascript - 如何在我的 React Native 应用程序中添加 NativeBase 页脚?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59486309/

相关文章:

javascript - Jquery滚动以在页面加载时偏移目标div

javascript - 使用 Javascript SVG 画线时,鼠标移动时会生成多个 <SVG>。如何确保只剩下最后一个 <SVG> ?

java - 使用 Android Studio 运行应用程序时出错

java - 如果设置准确性,为什么 Android LocationManager 在位置更新开始之前会有很长时间的延迟

javascript - 如何从 Jest 测试套件中排除 CSS 模块文件?

html - 一个接一个的CSS定位div

javascript - 在完整日历中添加一个按钮

javascript - 从嵌套 JSON 检索数据到 Morris.js xkey

javascript - 关于生命周期方法和 react 渲染?

android,如何从另一个 .xml 文件加载 linearlayout?