我是 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问题引起的。
因此,我做了一些研究并得到了这个解决方案。
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/