javascript - 在 TabBar 中移动选项卡时组件生命周期 (ComponentDidMount) 不起作用?
问题描述
当我使用react-native-tab-navigator
这些导航器的库移动页面以移动componentDidMount
页面时,它只能完成一次,之后,生命周期组件不起作用,请寻求解决方案:按照我的代码。
我在 Github 上打开了一个问题,我尝试将状态发送给父级。我用过
react-native-tab-navigator 版本 0.3.4
class MainTab extends Component {
state = {
selectedTab: 'home'
};
render() {
return (
<
TabNavigator.Item selected = {
this.state.selectedTab === 'home'
}
title = "Home"
selectedTitleStyle = {
{
color: "#FF7158",
}
}
tabStyle = {
{
borderTopWidth: this.state.selectedTab === 'home' ? 3 : 0,
borderTopColor: '#FF7158',
backgroundColor: this.state.selectedTab === 'home' ? '#fff8f6' : '#FFFFFF'
}
}
renderIcon = {
() => < Image source = {
require('allComponents/images/ic_beranda.png')
}
style = {
{
width: 18,
height: 18
}
}
/>}
renderSelectedIcon = {
() => < Image source = {
require('allComponents/images/ic_beranda-actives.png')
}
style = {
{
width: 18,
height: 18
}
}
/>}
// renderBadge={() => <View style={{width: 20, height:20, backgroundColor:'#FF7158', borderRadius:50}}><Text style={{fontSize:12, textAlign:'center', color:'white', fontWeight:'600'}}>2}
onPress = {
() => this.setState({
selectedTab: 'home'
})
} >
<
/TabNavigator.Item> <
TabNavigator.Item
selected = {
this.state.selectedTab === 'profile'
}
title = "Pemesanan"
selectedTitleStyle = {
{
color: "#FF7158",
}
}
tabStyle = {
{
borderTopWidth: this.state.selectedTab === 'profile' ? 3 : 0,
borderTopColor: '#FF7158',
backgroundColor: this.state.selectedTab === 'profile' ? '#fff8f6' : '#FFFFFF'
}
}
renderIcon = {
() => < Image source = {
require('allComponents/images/ic_pemesanan-inactive.png')
}
resizeMode = 'stretch'
style = {
{
width: 18,
height: 18
}
}
/>}
renderSelectedIcon = {
() => < Image source = {
require('allComponents/images/ic_pemesanan-active.png')
}
resizeMode = 'stretch'
style = {
{
width: 18,
height: 18
}
}
/>}
onPress = {
() => this.setState({
selectedTab: 'profile'
})
} >
<
/TabNavigator.Item> <
TabNavigator.Item
selected = {
this.state.selectedTab === 'riwayat'
}
title = "Akun"
selectedTitleStyle = {
{
color: "#FF7158",
}
}
tabStyle = {
{
borderTopWidth: this.state.selectedTab === 'riwayat' ? 3 : 0,
borderTopColor: '#FF7158',
backgroundColor: this.state.selectedTab === 'riwayat' ? '#fff8f6' : '#FFFFFF'
}
}
renderIcon = {
() => < Image source = {
require('allComponents/images/ic_akun-inactive.png')
}
resizeMode = 'stretch'
style = {
{
width: 18,
height: 18
}
}
/>}
renderSelectedIcon = {
() => < Image source = {
require('allComponents/images/ic_akun-active.png')
}
resizeMode = 'stretch'
style = {
{
width: 18,
height: 18
}
}
/>}
onPress = {
() => this.setState({
selectedTab: 'riwayat'
})
} >
<
/TabNavigator.Item>
);
}
}
我希望它componentDidMount
可以在 TabBar 上运行。
解决方案
componentDidMount
当它在 tabNavigator 内时不会触发(预计第一次安装组件)。
原因是当从一个选项卡切换到另一个选项卡时,每个选项卡都会第一次呈现,而所有选项卡都呈现出来而不卸载它们。
我不知道您使用的是哪个导航器,但通常您有办法知道屏幕何时“聚焦”以及何时“模糊”。当屏幕从模糊变为聚焦时,使用它们可以触发功能。
推荐阅读
- outlook - 打印 Outlook 项目时隐藏 CustomProperty
- xml - 用 pyspark 中的值包装 rdd.saveAsTextFile 的输出
- xslt - 如何遍历多个 XML 输入文件并将相关的属性值复制到 HTML 输出表中的一行
- python - Python 和 OpenCV:在保持纵横比的同时将图像大小调整为正方形
- c# - 3D 体素网格视线 Bresenham 算法
- c - C - 奇怪的 fork() + execl() 行为
- xaml - 设置左上角的后退按钮与 Xamarin.Forms 中的徽标对齐
- python - Python Flask - 避免在动态表返回渲染模板上发布
- c# - ii 具有不同参数的相同方法名称时的 ASP.NET 路由问题
- java - Java:Collections.sort 不起作用属性?