2016-11-08 91 views
0

我正在使用“导航器”并通过路由呈现多个组件。我想在渲染到组件之前显示加载器(如任何图像或文本“加载...”),因为这些都需要时间加载。请检查我的代码,并建议我在哪里使用装载机。 Route.js组件加载时间

import React from 'react'; 
import { 
    Platform, 
    StyleSheet, 
    Text, 
    View, 
    Image, 
    ListView, 
    TouchableHighlight, 
    Navigator, 
    TouchableOpacity, InteractionManager 
} from 'react-native'; 

import Home from './Home'; 
import Tomo from './App'; 
import Profile from './Profile'; 

var SCREEN_WIDTH = require('Dimensions').get('window').width; 
var BaseConfig = Navigator.SceneConfigs.FloatFromRight; 
var CustomLeftToRightGesture = Object.assign({}, BaseConfig.gestures.pop, { 
    snapVelocity: 10, 
    edgeHitWidth: SCREEN_WIDTH, 
}); 
var CustomSceneConfig = Object.assign({}, BaseConfig, { 
    springTension: 300, 
    springFriction: 10, 
    gestures: { 
    pop: CustomLeftToRightGesture, 
} 
}); 

export default class Route extends React.Component { 
    constructor(props, context) { 
    super(props, context); 
    this.state = {renderPlaceholderOnly: true}; 
    } 

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

    _configureScene(route) { 
    return CustomSceneConfig; 
    } 

    render() { 
    if (this.state.renderPlaceholderOnly) { 
     return this._renderPlaceholderView(); 
    } 
    return (
     <Navigator 
      initialRoute={{id: 'Home'}} 
      configureScene={this._configureScene} 
      renderScene={this.navigatorRenderScene} 

     /> 
    ); 
    } 

    _renderPlaceholderView() { 
    return (
     <View> 
     <Text>Loading...</Text> 
     </View> 
    ); 
    } 

    header() { 
    return (
     <View> 
     <Text>Loading...</Text> 
     </View> 
    ); 
    } 

    navigatorRenderScene(route, navigator) { 
    _navigator = navigator; 
    switch (route.id) { 
    case 'Home': 
     return (<Home navigator={navigator} title="Home"/>); 
    case 'Tomo': 
     return (<Tomo navigator={navigator} title="Tomo"/>); 
    case 'Profile': 
     return (<Profile navigator={navigator} title="Profile" />); 
    } 
    } 
} 

在此先感谢。

+0

你是否在加载组件之前做任何网络操作? – Jickson

+0

不,只是渲染组件。包含更多文件的组件之一,以便花费时间加载。 –

+0

好的。您发布的代码没有问题。您可以发布在从源组件到目标组件导航时引起延迟的组件代码。 – Jickson

回答

0

创建一个基础组件作为其他组件的容器(Home,Tomo,Profile)。该容器还包含一个Loading组件。因此,此容器中的每个组件都将具有加载效果。这样的关键代码:

class BaseContainer extends Component { 
    constructor(props) { 
    super(props); 
    this.state = { 
     showLoading: true 
    } 
    } 

    render() { 
    ...... 

    return (
     {this.props.children} 
     { 
     this.state.showLoading ? <Loading /> : '' 
     } 
    ); 
    } 

    ...... 
} 
+0

感谢杰森,我是新的反应本地和想知道我应该使用具有导航代码Route.js文件:渲染(){\t navigatorRenderScene (路线,导航器){__ navigator =导航器; switch(route.id){\t \t case'Home': return(); case'Tomo': return(); case'Profile': return(); } } –

+0

什么时候应该保持false showLoading状态(this.state.showLoading)? –

+0

您是否使用某些组件进行路由?最好为你的应用的路由选择一个路由,比如'react-native-router'(https://github.com/t4t5/react-native-router)。当你完成耗时的事情时,只需将showLoading状态改为false,组件就会再次呈现,而不需要加载组件。 –