Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to perform a function in header? #1468

Closed
hardtosaygoodbye opened this issue May 11, 2017 · 8 comments
Closed

how to perform a function in header? #1468

hardtosaygoodbye opened this issue May 11, 2017 · 8 comments
Labels

Comments

@hardtosaygoodbye
Copy link

| software | version
| react-navigation 1.0.0
| react-native 0.44
| node 7.3

import React from 'react';
import {
AppRegistry,
Text,
Button,
View
} from 'react-native';
import { StackNavigator } from 'react-navigation';

class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
headerLeft: <Button title='button' onPress={() => {this.changeText()}} //error
/>
};
constructor(props){
super(props)

this.state = {
  text: '11'
}

}
changeText(){
this.setState({ text: '22'})
}
render() {
return (

{this.state.text}
<Button
title='changeText'
onPress={() => {this.changeText()}}
/> //ok

)
}
}

const test = StackNavigator({
Home: { screen: HomeScreen },
});

AppRegistry.registerComponent('test', () => test);

This question is from the previous issues, but the answer is wrong, who can help me ?

@sunnylqm
Copy link

sunnylqm commented May 14, 2017

A simple way is to store this outside class, but this has side effects on several instances.

let _this = null;

export default class WebViewPage extends Component {
    static navigationOptions = ({ navigation }) => ({
        // ......
                    onPress={() => _this.reload() }>
        // ......
        )
    });
   componentDidMount() {
       _this = this;
   }
    _reload() {
        console.log("aaaaaaa");
    }

A better (and of course more complex) way is to use navigation.setParams to put methods into navigation.state.params and then invoke them in your navigationOptions.

@dzpt
Copy link

dzpt commented Jun 25, 2017

@sunnylqm It doesn't work.
First of all, you declared _this as a const, it means it immutable.

@sunnylqm
Copy link

sunnylqm commented Jun 25, 2017

@tom29 My mistake. Simply replace const with let.

@buncis
Copy link

buncis commented Jul 16, 2017

is @sunnylqm code considered as good practices?

@shivanraptor
Copy link

Codes of @sunnylqm work, but it doesn't look like a clean way to do so. Any alternatives?

@spencercarli
Copy link
Member

Hi! In an effort to get the hundreds of issues on this repo under control I'm closing issues tagged as questions. Please don't take this personally - it's simply something we need to do to get the issues to a manageable point. If you still have a question I encourage you to re-read the docs, ask on StackOverflow, or ask on the #react-navigation Reactiflux channel. Thank you!

@yasir-netlinks
Copy link

You can use setParams

static navigationOptions = ({ navigation }) => {
    headerRight: navigation.state.params && navigation.state.params.headerRight
}

componentDidMount(){
     this.props.navigation.setParams({
            headerRight: (<TouchableOpacity onPress={this._headerRightPressed}></TouchableOpacity>)
     })
}

@brentvatne
Copy link
Member

@react-navigation react-navigation locked and limited conversation to collaborators Jun 11, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

8 participants