React Native使用react-navigation時,設置navigationOptions中Static中使用this注意點


使用react-navigation時,單頁面設置navigationOptions中,進行Static中調用方法,不能像以下設置

onPress = {()=>this.clickFinishButton()}
export default class extends Component {
  static navigationOptions = ({
    navigation,
    screenProps
  }) => ({
    headerTitle: 'List實現多選',
    headerTitleStyle: {
      color: 'white'
    },
    headerStyle: {
      backgroundColor: Color.kMainColor // 設置導航欄的背景顏色,headerTintColor設置無效
    },
    headerRight:(
        <NavigationItem
            title='完成'
            // 這里注意: static里面不能使用this調用方法,出現clickFinishButton is not function
            // 參考博客: http://www.jianshu.com/p/2f575cc35780
            // onPress={()=>navigation.state.params.navigatePress()}
            onPress = {()=>this.clickFinishButton()}
        />
    )
  });

解決方法:

export default class extends Component {
  static navigationOptions = ({
    navigation,
    screenProps
  }) => ({
    headerTitle: 'List實現多選',
    headerTitleStyle: {
      color: 'white'
    },
    headerStyle: {
      backgroundColor: Color.kMainColor // 設置導航欄的背景顏色,headerTintColor設置無效
    },
    headerRight:(
        <NavigationItem
            title='完成'
            // 這里注意: static里面不能使用this調用方法,出現clickFinishButton is not function
            // 參考博客: http://www.jianshu.com/p/2f575cc35780
            onPress={()=>navigation.state.params.navigatePress()}
            // onPress = {()=>this.clickFinishButton()}
        />
    )
  });
componentDidMount(){
    // 處理數據源
    this.handlerDataSource();
    this.props.navigation.setParams({navigatePress:this.clickFinishButton})
  }
  // 點擊完成按鈕
  clickFinishButton = ()=> {
      alert('哈哈');
    //   let data = this.state.dataArr;
    //   let selectResultArr = [];
    //   for (var index in data) {
    //         var element = object[index];
    //         if (element.isSelected) {
    //             selectResultArr.push(element);
    //         }
    //   }
    //   alert(selectResultArr.length);
  }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM