使用React-Native中的Navigator组件进行自定义导航

JavaScript

猴子乐

2020-03-11

I’m exploring possibilities of React Native while developing a demo app with custom navigation between views with the help of Navigator component.

The main app class renders navigator and inside renderScene returns passed component:

class App extends React.Component {
    render() {
        return (
            <Navigator
                initialRoute={{name: 'WelcomeView', component: WelcomeView}}
                configureScene={() => {
                    return Navigator.SceneConfigs.FloatFromRight;
                }}
                renderScene={(route, navigator) => {
                    // count the number of func calls
                    console.log(route, navigator); 

                    if (route.component) {
                        return React.createElement(route.component, { navigator });
                    }
                }}
             />
        );
    }
}

For now app contains two views:

class FeedView extends React.Component {
    render() {
        return (
            <View style={styles.container}>
                <Text>
                    Feed View!
                </Text>
            </View>
        );
    }
}

class WelcomeView extends React.Component {
    onPressFeed() {
        this.props.navigator.push({
            name: 'FeedView',
            component: FeedView
        });
    }

    render() {
        return (
            <View style={styles.container}>
                <Text style={styles.welcome}>
                    Welcome View!
                </Text>

                <Text onPress={this.onPressFeed.bind(this)}>
                    Go to feed!
                </Text>
            </View>
        );
    }
}

What I want to figure out is:

  • I see in the logs that when pressing “go to feed” renderScene is called several times though the view renders correctly once. Is it how the animation works?

    index.ios.js:57 Object {name: 'WelcomeView', component: function}
    index.ios.js:57 Object {name: 'FeedView', component: function}
    // renders Feed View
    
  • Generally does my approach conform to the React way, or can it be done better?

What I want to achieve is something similar to NavigatorIOS but without the navigation bar (however some views will have their own custom navigation bar).

第658篇《使用React-Native中的Navigator组件进行自定义导航》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

3个回答
前端凯神乐 2020.03.11

Navigator现在已弃用,RN 0.44.0您可以react-native-deprecated-custom-components改用来支持使用的现有应用程序Navigator

猴子十三 2020.03.11

现在不推荐使用Navigator组件。您可以使用askonov的react-native-router-flux,它种类繁多,支持许多不同的动画,并且效率很高

Stafan猿 2020.03.11

您的方法应该很好。在Fb的大型应用程序中,我们避免在require()渲染之前为场景组件调用,这可以节省一些启动时间。

renderScene首次将场景推送到导航器时,应调用函数。当导航器重新渲染时,也将为活动场景调用该视图。如果您renderScene在之后看到多次被调用push,则可能是一个错误。

导航器仍在开发中,但是如果发现任何问题,请在github上归档并标记我!(@ericvicenti)

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android