how to use InteractionManager.runAfterInteractions make navigator transitions faster

情到浓时终转凉″ 提交于 2019-12-21 03:37:47

问题


Because of complex logic, I have to render many components when this.props.navigator.push(), slow navigator transitions make app unavailable.

then I notice here provide InteractionManager.runAfterInteractions api to solve this problem,

I need bring most of components which consumed long time to callback after navigator animation finished, but I don't know where should I call it,

maybe a simple example is enough,

thanks for your time.


回答1:


Here's a full example of what a performant Navigator scene might look like:

import {Component} from 'react';
import {InteractionManager, Text} from 'react-native';

class OptimizedScene extends Component {

  state = {interactionsComplete: false};

  componentDidMount() {
    InteractionManager.runAfterInteractions(() => {
      this.setState({interactionsComplete: true});
    });
  }

  render() {
    if (!this.state.interactionsComplete) {
      return <Text>loading...</Text>;
    }

    return (
      <ExpensiveComponent/>
    );
  }
}

This has been extracted into a library to make it even easier:

import {AfterInteractions} from 'react-native-interactions';

function MyScene() {
  return (
    <AfterInteractions placeholder={<CheapPlaceholder/>}>
      <ExpensiveComponent/>
    </AfterInteractions>
  );
}



回答2:


Take a look at https://facebook.github.io/react-native/docs/performance.html#slow-navigator-transitions

There you can find an example of how to implement placeholders to get faster transitions!




回答3:


You should pass any code that is keeping the JS thread busy to the InteractionManager, e.g.

InteractionManager.runAfterInteractions(() => {
    someLongTask() // or animations, or whatever
})



回答4:


import {InteractionManager} from "react-native"; 

componentDidMount() {
    InteractionManager.runAfterInteractions(() => {
      this.setState({renderPlaceholderOnly: false});
    });
  }

Reference : https://facebook.github.io/react-native/docs/performance.html#slow-navigator-transitions



来源:https://stackoverflow.com/questions/36127185/how-to-use-interactionmanager-runafterinteractions-make-navigator-transitions-fa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!