如何使用react-native-router-flux在导航栏中制作右键?

时间:2016-08-19 08:22:12

标签: android ios react-native react-native-router-flux

我想使用react-native-router-flux在导航栏上制作右键。

我的代码是

import React from 'React';
import { TouchableHighlight } from 'react-native';
import { Scene, Router, Actions } from 'react-native-router-flux';
...
const filterIcon = () => (
<TouchableHighlight onPress={()=>} style={{...}}>
<Icon name="filter" size={30}/>
</TouchableHighlight>
);
const MainRounter=()=>(
<Router>
<Scene
key="main"
component={mainPage}
initial={true}
renderRightButton={()=>filterIcon}
/>
</Router>
);

但我无法在导航栏上显示右键。 我该怎么做?

6 个答案:

答案 0 :(得分:9)

您可以使用onRightrightTitlerightButtonImage将右键添加到导航栏。

const MainRounter = return (
    <Router>
        <Scene
            key="main"
            component={mainPage}
            initial={true}
            onRight={ ()=> whatever you want to do }
            rightButtonImage={require('path/to/your/icon')}
        />
    </Router>
);

让我知道它是否有效:)

答案 1 :(得分:6)

更新 - 2017年10月18日

在最后一个版本中,下面描述的方法再次停止工作。所以,我的最后一个解决方案是:

class MyAwesomeClass extends React.Component {


   componentWillMount() {
        Actions.refresh({ right: this._renderRightButton });
   }

   _renderRightButton = () => {
        return(
            <TouchableOpacity onPress={() => this._handleIconTouch() } >
                <Icon name="check" size={26} color='grey' />
            </TouchableOpacity>
        );
    };

    _handleIconTouch = () => {
        console.log('Touched!');
    }

}

更新 - 2017年8月7日

由于RNRF4现在使用ReactNavigation作为导航解决方案,上述解决方案停止工作。为了使它再次工作,我直接使用ReactNavigation API。这是我的解决方案:

class MyAwesomeClass extends React.Component {
    static navigationOptions = ({ navigation }) => {
        headerRight: <TouchableIcon size={22} name="search" onPress={() =>    
                        navigation.state.params.handleIconTouch()} />,
    }

    componentWillMount() {
        this.props.navigation.setParams({ handleIconTouch: 
              this.handleIconTouch });

    }

    handleIconTouch = () => {
        console.log('Touched!');
    }

}

在navigationOptions中,一般导航信息在类级别中加入。由于上下文是静态的,因此无法访问我们的Component方法及其参数。为了使它工作,需要一些解决方法。

因此,首先需要定义将处理触摸的函数,在本例中为handleIconTouch。在这里,您应该定义按钮按下时将执行的操作。由于此功能不是静态的,您可以从此处访问道具和状态,从而为您提供更多功能和选项。

然后,在 componentWillMount()方法中,将方法添加到导航参数,并使用 setParams()在navigationOptions的静态上下文中使用。由于此方法也不是静态的,您可以在此处将任何所需的参数传递给您的按钮。

最后,使用参数,使用最适合您的组件在navigationOptions / headerRight键中定义按钮。

<强> ORIGINAL 我推荐你这种方法:

  1. 使用所需的逻辑和图像创建方法,例如:

    renderRightButton = () => {
        return(
            <TouchableOpacity onPress={() => null } >
                <Icon name="check" size={26} color='grey' />
            </TouchableOpacity>
        );
    };
    
  2. 在您的componentWillMount()方法中添加开头:

    Actions.refresh({ renderRightButton: this.renderRightButton });

  3. 准备好了!

  4. 如果您有任何疑问,请告诉我。

答案 2 :(得分:2)

对于RNRF4。谢谢Jacse

import React from 'react';
import { View, Button, Alert } from 'react-native';

class MyScreen extends React.Component {
    static navigationOptions = ({ navigation }) => {
      const { params = {} } = navigation.state;
      return {
        headerRight: <Button title="Save" onPress={() => params.handleSave()} />
      };
    };

    _saveDetails() {
      Alert.alert('clicked save');
    }

    componentDidMount() {
      this.props.navigation.setParams({ handleSave: this._saveDetails });
    }

    render() {
      return (
        <View />
      );
    }
}
export default MyScreen;

答案 3 :(得分:2)

RNRF4我试过这段代码工作正常

   <Scene 
         key="SignUp" 
         component={SignUp} 
         title="SignUp"
         onRight={()=>{}} 
         rightTitle={' Save'} />

// SignUp Component

export default class SignUp extends Component {
    componentWillMount() {
        this.props.navigation.setParams({
            'onRight': this.handleIconTouch
        })
    }

    handleIconTouch() {
        debugger;
    }

    render() {
        return ();
    }
}

答案 4 :(得分:0)

你应该创建一个自定义的navBar来看看github custom navBar中的这个问题

答案 5 :(得分:0)

2021 年更新:

这将删除弃用警告。

HomePage.navigationOptions = () => {
  return {
    headerRight: () => (
      <TouchableOpacity onPress={() => navigation.navigate('Create')}>
        <Feather name="plus" size={30} />
      </TouchableOpacity>
    )  };
};

这里的 Feather 是一个在应用程序中显示图标的库,它在 Expo 中可用。

您可以通过像下面这样导入来使用这个库。

import { Feather } from '@expo/vector-icons'
相关问题