React Native:如何将道具传递给' routeMapper' ' Navigator.NavigationBar'?

时间:2016-01-11 20:05:22

标签: javascript reactjs react-native

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

我的代码结构如下

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>
        );
}

1 个答案:

答案 0 :(得分:10)

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

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>
        );
}