【问题标题】:React Native: How to pass props to 'routeMapper' of 'Navigator.NavigationBar'?React Native:如何将道具传递给'Navigator.NavigationBar'的'routeMapper'?
【发布时间】:2016-01-11 20:05:22
【问题描述】:

我在使用 React Native 0.16 (Android) 时遇到了麻烦。 问题是如何将props传递给属于<Navigator />组件的Navigator.NavigationBarrouteMapper

我的代码结构如下

class MyApp extends Component {    
    ...

    static NavigationBarRouteMapper = {
        LeftButton(route, navigator, index, navState) {
            // ### I want to call 'functionABC' here. What Should I do?
        },
        RightButton(route, navigator, index, navState) {
            // ### I want to call 'functionABC' here. What Should I do?
        },            
        Title(route, navigator, index, navState) {
            // ### I want to call 'functionABC' here. What Should I do?
        },           
    }       

    ...

    functionABC() {
       ...
    }      

    ...

    render() {
        return (
            <View>
              <Navigator
                 initialRoute={{ 
                 name: 'Main',
                 index: 0,
                 }}
                 renderScene={this.renderScene.bind(this)}
                 sceneStyle={{marginTop:50}}
                 navigationBar={
                   <Navigator.NavigationBar
                     routeMapper={MyApp.NavigationBarRouteMapper}
                   />
                }
                 ref="nav" />
            </View>
        );
}

【问题讨论】:

    标签: javascript reactjs react-native


    【解决方案1】:

    您可以在每次使用函数时生成映射器对象,在您的代码中这可能只是:

    class MyApp extends Component {    
        ...
    
        static NavigationBarRouteMapper = props => ({
            LeftButton(route, navigator, index, navState) {
            },
            RightButton(route, navigator, index, navState) {
            },            
            Title(route, navigator, index, navState) {
            },           
        })       
    
        render() {
            return (
                <View>
                  <Navigator
                     initialRoute={{ 
                     name: 'Main',
                     index: 0,
                     }}
                     renderScene={this.renderScene.bind(this)}
                     sceneStyle={{marginTop:50}}
                     navigationBar={
                       <Navigator.NavigationBar
                         routeMapper={MyApp.NavigationBarRouteMapper(this.props)}
                       />
                    }
                     ref="nav" />
                </View>
            );
    }
    

    【讨论】:

    • 谢谢你,@gre。有效。我想我应该多学习 ES6 语法。
    猜你喜欢
    • 1970-01-01
    • 2018-08-25
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多