gpt4 book ai didi

javascript - 在 TabBar 中移动选项卡时,组件生命周期 (ComponentDidMount) 不起作用?

转载 作者:行者123 更新时间:2023-11-30 19:26:11 25 4
gpt4 key购买 nike

当我使用这些导航器的库 react-native-tab-navigator 移动页面以移动 componentDidMount 页面时,它只能完成一次,之后,生命周期组件不起作用,寻求解决方案:按照我的代码。

我在 Github 上开了一个问题,我试过将状态发送给 parent 。我用过

react-native-tab-navigator version 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 内时不会触发(预计第一次安装组件)。

原因是当从一个选项卡切换到另一个选项卡时,每个选项卡都会第一次呈现,留下所有呈现的选项卡而不卸载它们。

我不知道您使用的是哪个导航器,但通常您有办法知道屏幕何时“聚焦”以及何时“模糊”。当屏幕从模糊变为聚焦时,您可以使用它们触发功能。

关于javascript - 在 TabBar 中移动选项卡时,组件生命周期 (ComponentDidMount) 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56881834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com