react native在static中使用this方法


方法一
首先需要在componentDidMount(){}中動態的添加點擊事件

屬性給params

componentDidMount(){ this.props.navigation.setParams({ title:'Save,
 navigatePress:this.clickSave }) }
clickSave = () => { alert('點擊headerRight中的保存'); console.log(this.props.navigation); }
接下來就可以通過params方法來獲取點擊事件了
static navigationOptions = ({ navigation}) => ({ title: navigation.state.params.title, headerRight:( <Text onPress={() => navigation.state.params.navigatePress()}>
 保存 </Text> ) });
 方法二
let _this = null    // 在class之前定義 export default class MineScene extends Component {   static navigationOptions = ({ navigation }) => ({
    // ......       onPress={() => _this.clickSave() }
    // ...... })
  componentDidMount() {     _this = this }
  clickSave = () => {
    alert('點擊headerRight中的保存');
    console.log(this.props.navigation);
  }

// ......
// ......
// ......

 


免責聲明!

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



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