gpt4 book ai didi

react-native - 从一个选项卡移动到另一个选项卡并返回第一个选项卡后,如何重新渲染屏幕及其所有组件?

转载 作者:行者123 更新时间:2023-12-04 04:19:32 26 4
gpt4 key购买 nike

每当用户按下一个选项卡或从一个选项卡移动到另一个选项卡然后再到第一个选项卡时,我都会尝试重新渲染屏幕。我还在屏幕上放置了一个自定义标题。所有其他选项卡上的相同标题也从异步存储获取一些状态,但一旦屏幕加载,它也不会刷新。我正在使用响应式(Reactive)导航。导航中是否有任何方法会在选项卡聚焦时调用。

const AppStack = createBottomTabNavigator(
{
Search: SearchScreen,
Saved: SavedScreen,
Explore: ExploreStack,
Offers: OffersScreen,
Profile: ProfileScreen,
},
{
initialRouteName: 'Explore',
navigationOptions: ({navigation})=>({
tabBarIcon: ({focused, tintColor})=>{
const { routeName } = navigation.state;
let iconName, iconSize;
switch(routeName) {
case "Search":
iconName = `ios-search${focused ? '' : '-outline'}`;
break;
case "Saved":
iconName = `ios-heart${focused ? '' : '-outline'}`;
break;
case "Explore":
iconName = `ios-navigate${focused ? '' : '-outline'}`;
break;
case "Offers":
iconName = `ios-pricetag${focused ? '' : '-outline'}`;
break;
case "Profile":
iconName = `ios-person${focused ? '' : '-outline'}`;
break;
default:
break;
}
return <Icon name={iconName} color={tintColor} />;
},
}),
tabBarOptions: {
activeTintColor: 'black',
inactiveTintColor: 'grey',
activeBackgroundColor: '#abaf9b',
labelStyle: {
fontSize: 15,
},
// style for tabbar
style: {
backgroundColor: '#ffffff',
height: 60,
justifyContent: 'space-around',
alignContent: 'center',
alignItems: 'center',
},
// style for tab
tabStyle: {
paddingTop: 7,
paddingBottom: 7
}
},
}
)

这是选项卡之一。其他选项卡使用相同的组件但 API 不同。

import React, { Component } from 'react';
import { View, Image, ActivityIndicator, TouchableWithoutFeedback, TouchableHighlight, AsyncStorage } from 'react-native';
import HeaderComponent from '../components/Header';
import SomeComponent from '../components/Some';
import { Container, Content, Icon, Spinner} from 'native-base';

class FirstScreen extends Component{

constructor(props){
super(props)
this.state = {
somelist: [],
name: '',
userId: '',
isloading: true,
location: ''
};
this.getUser();
}

componentDidMount(){
this.getLocation();
}

getLocation = async() => {
const result = await AsyncStorage.getItem('location');
console.log("Location " +result)
this.setState({location: result});
}

getUser = async() => {
const result = await AsyncStorage.getItem('user');
const data = JSON.parse(result);
console.log("data : "+data)
this.setState({name: data.name, userId: data.userId})
}

componentWillMount(){
console.log("Component will mount")
//For demo
fetch('http://someapi.co/api/listing')
.then(response => response.json())
.then(data => {
this.setState({ somelist: data, isloading: false }, function(){console.log(this.state.somelist, this.state.isloading)})
})
.catch(function(error){
console.log("Error : "+error);
});

//console.log(this.state.barlist);
}

renderComponent(){

if(this.state.isloading == true){
return (
<View style={{ flex: 1, justifyContent: 'center', height: 300 }}>
<ActivityIndicator size="large" color="#0000ff" />
</View>
);
}
return this.state.somelist.map( user=>
<SomeComponent key={user.id}
onPress={()=>this.props.navigation.navigate('OtherScreen', {'Id': user.id, 'userId': this.state.userId})}

image={user.image[0].imageName}
/>
);
}

render(){
console.log(this.state.userId)
return (
<Container>
<HeaderComponent
profilePress={()=>this.props.navigation.navigate('Profile')}
seachPress={()=>this.props.navigation.navigate('SearchSetting')}
// location={this.state.location}
/>
<TouchableHighlight
style={{position: 'absolute', bottom: 20, zIndex:999999, right: 20 }}
onPress={()=>this.props.navigation.navigate('Map')}
>
<Image source={require('../images/navigation_icon.png')} style={{height: 50, width: 50}}/>
</TouchableHighlight>
<Content>
<View style={{padding: 0, margin: 0}}>
{this.renderComponent()}
</View>
</Content>
</Container>
);
}
}

export { SomeScreen };

最佳答案

您可以在react-navigation 中访问事件监听器,如前所述here

// call when the screen is focused
componentDidMount () {
this._onFocusListener = this.props.navigation.addListener('didFocus', (payload) => {
// refresh the component here
// or update based on your requirements
});
}

关于react-native - 从一个选项卡移动到另一个选项卡并返回第一个选项卡后,如何重新渲染屏幕及其所有组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52053671/

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