【问题标题】:How to forcefully unmount a component in react native?如何在本机反应中强制卸载组件?
【发布时间】:2018-02-21 12:43:17
【问题描述】:

我使用reactnavigationstackNavigtor 作为根结构。

当我的应用程序加载时,它最初会根据 intialRouteName 安装一个组件 - 到目前为止这很好。但是,当我打开我的 slideMenu 并导航到另一个屏幕时,我刚刚进入的组件并没有卸载,并且仍然在后面呈现,即使我已经成功导航了。

当然,我可以在我的初始组件中使用this.state({}) 来停止渲染,但问题是,我无法将其设置为当我导航到另一个屏幕时,我会阻止渲染上一屏。

AppNavigation.js

/*
 * LaunchScreen loads first
 * I then navigate to Profile via (this.props.navigate('OpenDrawer')).
 *  - This opens CustomComponent and inside that, I select Profile
 * When I navigate to Profile, The LaunchScreen is not unmounted
*/
export const SignedIn = StackNavigator({
  LaunchScreen: {
    screen: $LaunchScreen
  },
  Profile: {
    screen: $Profile
  },
  EditProfile: {
    screen: EditProfile
  },
  MyDoctors: {
    screen: $Doctors
  }
}, {
  headerMode: "none",
  mode: "modal",
  initialRouteName: 'LaunchScreen' // this is the component that loads
});

export const HomeNav = StackNavigator({
  Home: {
    screen: SignedIn,
    navigationOptions: {
      gesturesEnabled: false
    }
  }
}, {
  headerMode: "none",
  mode: "modal",
  initialRouteName: "Home", // we enter this route when the app loads.
});

const PrimaryNav = StackNavigator({
  SignedIn: {
    screen: HomeNav,
    navigationOptions: {
      gesturesEnabled: false
    }
  },
  SignedOut: {
    screen: SignedOut,
    navigationOptions: {
      gesturesEnabled: false
    }
  },
  Loading: {
    screen: Loading
  }
}, {
  headerMode: "none",
  mode: "modal",
  initialRouteName: "Loading",
});

export default PrimaryNav;

LaunchScreen.js

// ... Imports {}


export default class LaunchScreen extends PureComponent<*, State> {
  constructor(props) {
    super(props);
    this.state = {
      index: 0,
    };
  }

  componentDidMount() {
  };

_renderScene = ({ route }) => {
    return (
      <SimplePage
        state           = {this.state}
        style           = {{ backgroundColor: 'white' }}
        type            = {this.state.type}
        updateIndex     = {this.updateIndex.bind(this)}

      />
    );
  };
  
  _renderHeader = props => {
  /*
   * "this.props.navigation.navigate('DrawerOpen')" opens the drawer from left side
   *
  */
    return (
      <View>
        <View style={styles.headerContainer}>
          <TouchableHighlight onPress={() => this.props.navigation.navigate('DrawerOpen')} activeOpacity={1.0} underlayColor="rgba(253,138,94,0)">
          </TouchableHighlight>
        </View>
      </View>
    );
  };


  render() {
    return (
        <TabViewAnimated
        style={[styles.container, this.props.style]}
        navigationState={this.state}
        renderScene={this._renderScene}
        renderHeader={this._renderHeader}
      />
    );
  }
}

主要问题是什么?

我想知道当我导航到另一个屏幕时如何卸载组件。

【问题讨论】:

  • 在堆栈导航中,我们不断将页面/视图推送到堆栈,并且在我们移动到另一个堆栈之前,这些页面/视图不会卸载。所以你不能这样卸载。
  • 啊,我明白了,在那种情况下,我们如何创建两个不同的堆栈?并链接它们? @帕万
  • 您的堆栈将包含前一个屏幕,并且不会被卸载。但是你为什么要卸载呢?
  • 因为初始组件正在渲染大量内容,当我导航到不同的屏幕时,之前屏幕的渲染内容仍然存在并且严重影响性能@Pavan

标签: javascript reactjs react-native react-navigation react-native-navigation


【解决方案1】:

你可以使用这样的条件:

render() {
  return (
    <View>
      <View style={styles.headerContainer}>
        { this.state.someCondition ?
          <SomeCoponent onPress={() => this.onPress}/> :
          <AnotherComponent onPress={() => this.onPress}/>
        }
      </View>
    </View>
  )
}

看看react-router

render() {
  return (
    <View>
      <View style={styles.headerContainer}>
        <Router>
          <Route exact path="/" component={HomeComponent}/>
          <Route path="/news" component={NewsFeedComponent}/>
          <Route exact path="/another" component={AnotherComponent}/>
        </Router>
      </View>
    </View>
  )
}

【讨论】:

  • 这不是一个不同的库吗?这意味着我必须将整个反应导航导航更改为路由器
  • 这取决于你。我刚才提到了。您需要分析它是否适合您的应用程序。
猜你喜欢
  • 2019-07-27
  • 1970-01-01
  • 2020-01-18
  • 2018-03-25
  • 2012-11-24
  • 1970-01-01
  • 1970-01-01
  • 2015-08-07
  • 1970-01-01
相关资源
最近更新 更多