简体   繁体   English

在 React Navigation v3 中单击 React Native 中 bottomTabNavigator 上的选项卡时,如何刷新我的组件?

[英]How do I refresh my component when clicked on tab on bottomTabNavigator in React Native, in React Navigation v3?

I am using react-navigation version 3, I have bottom tab navigator, have 4 tabs.我正在使用 react-navigation 版本 3,我有底部选项卡导航器,有 4 个选项卡。

out of which one is chat-screen, called as Chat , containing two views as:其中一个是聊天屏幕,称为Chat ,包含两个视图:

  1. main chat screen for chats or posts聊天或发帖的主聊天屏幕

  2. if not login then show with buttons for signup and login.如果没有登录,则显示注册和登录按钮。

when clicked on signup or login, it will take to respective screen for signing or logging in.单击注册或登录时,将进入相应的屏幕以进行登录或登录。

but from login/signin page when I click on bottom-tab navigator on Chat , it should again reload and check whether user is logged in or not?但是当我在Chat上单击底部选项卡导航器时,从登录/登录页面,它应该再次重新加载并检查用户是否登录?

problem is when I am navigating to signin/login page, from my tab, then It's not refreshing or called again or remounted.问题是当我从我的选项卡导航到登录/登录页面时,它不会刷新或再次调用或重新安装。

  1. my chat component is:我的聊天组件是:

     class ChatScreen extends React.Component { constructor(props) { super(props); this.state = { loading: true, refreshing: false } this.onRefresh = this.onRefresh.bind(this); let WebViewRef; } componentDidMount() { this.didFocusListener = this.props.navigation.addListener( 'didFocus', () => { console.log('bottom tab navigator is called'); }, ); if (this.props.userInfo) { this.get_access_token() } else { console.log(". this.props.userInfo ") } } render() { if (this.props.userInfo && this.state:access_token) return ( <SafeAreaView style={{ flex: 1 }}> <ScrollView contentContainerStyle={{ flex. 1 }} refreshControl={<RefreshControl refreshing={this.state.refreshing} onRefresh={this:onRefresh} />} > <View style={{ flex: 1 }}> <WebView source={{ uri. 'my-demosite-anywebsite?com.access_token=' + this.state.access_token }} onLoad={() => this:setState({ loading. false })} ref={WebViewRef => (this.WebViewRef = WebViewRef)} /> </View> </ScrollView> </SafeAreaView> ) else if (.this.props.userInfo) { return <MyCompanyLogoScreen /> } else { return <LoadingScreen /> } } } class MyCompanyLogoScreen extends React.Component{ render() { return ( <View style={styles.container}> {?this:state,request_to_login: <View> <MyCompanyLogoScreenAllComponents/> <Button bgColor="#4cd137" textColor="#FFFFFF" secondary rounded style={{ alignSelf, 'stretch': marginTop, hp(5): width. '35%', marginRight: wp(6) }} caption={'LOGIN UP'} onPress={() => this.navigateToLoginScreen(redirect_to_signup_or_login = "login") } /> </View> }

my problem is how can I refresh whole component when I tap on tab at bottomTabNavigator as Chat ?我的问题是当我点击 bottomTabNavigator 的选项卡作为Chat时如何刷新整个组件?

also didFocus is not working. didFocus 也不起作用。

you can use higher order component which passes the isFocused prop into a wrapped component.您可以使用高阶组件它将 isFocused道具传递到包装组件中。 example:例子:

import { withNavigationFocus } from 'react-navigation';

class TabLabel extends React.Component {

    componentDidUpdate(prevProps) {
      if (prevProps.isFocused !== this.props.isFocused) {
        //Call any action to update you view
        //fetch data when the component is focused
        //To prevent extra component re-renders, you may need to write some logic in shouldComponentUpdate
        }
      }

  render() {
    return <Text>{this.props.isFocused ? 'Focused' : 'Not focused'}</Text>;
  }
}

// withNavigationFocus returns a component that wraps TabLabel and passes
// in the navigation prop
export default withNavigationFocus(TabLabel);

React navigation >= 6反应导航 >= 6

React Native Tab Navigation has an option prop as unmountOnBlur . React Native Tab Navigation 有一个选项unmountOnBlur Set it to true and it will reload the tab screens every time you click on tabs.将其设置为true ,每次单击选项卡时都会重新加载选项卡屏幕。

<Tab.Screen
  initialParams={{ ...params, article: null }}
  options={{
     title: "HomePage",
     unmountOnBlur: true,
  }}
  name="HomePage"
  component={ResenhaPage}
/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将路由添加到我的 react 本机应用程序导航,但将其排除在我的选项卡导航之外? - How do I add routes to my react native app navigation, but keep it out of my tab navigation? 如何将 Prop 传递给导航屏幕组件 - React Native - How do I pass a Prop to a Navigation Screen Component - React Native 如何在 React Native 的另一个组件下创建顶部导航选项卡? - How to Create Top Navigation Tab Under Another Component in React Native? 在 React Native Navigation 中,如何将道具发送到我的屏幕? - In React Native Navigation, how do I send props to my screens? 在React Native(和Ignite)中,如何创建一个ListView(在选项卡中),当单击列表项时,该View导航到另一个容器? - In React Native (and Ignite), how do I create a ListView (inside a tab) that navigates to another container when a list item is clicked? react native,react-navigation,TabNavigator,当我更改选项卡屏幕时如何发送请求 - react native, react-navigation, TabNavigator, how to send a request when I change tab screen 使用Navigation.push(来自wix的本地导航v2)我没有在目标组件中获取道具。 我如何访问道具? - With Navigation.push (v2 of react native navigation from wix) I am not getting props in destination component. How do I access props? react-native-navigation V3使应用程序崩溃 - react-native-navigation V3 crashing the app 在 BottomTabNavigator react-navigation v5 内嵌套 StackNavigator - Nesting StackNavigator inside BottomTabNavigator react-navigation v5 react-native bottomTabNavigator被Android系统导航栏部分覆盖 - react-native bottomTabNavigator is partially covered by Android system navigation bar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM