How to call a function in the render function?

Dedpul

If I try to call the fetchToken() function it just says that it is not a function. If I put it outside of the render function this.props is undefined and i'm not able to call it.

class LoginPage extends Component {


    componentDidMount() {
        Linking.addEventListener('url', this.handleOpenURL);
    }
    componentWillUnmount() {
        Linking.removeEventListener('url', this.handleOpenURL);
    }
    handleOpenURL(event) {
        let code = event.slice(22,86);
        console.log(code);
        this.fetchToken(code)
    }
  
  render() {

        function fetchToken(code) {
            this.props.actions.fetchToken(code)
        }
        
        return (
            <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
                <TouchableHighlight style={{backgroundColor: '#9b59b6', height: 70, padding: 20}} onPress={this.openAuth.bind(this)}>
                    <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
                        <Text style={{color: 'white', fontSize: 16}}>Authenticate with Dribbble</Text>
                    </View>
                </TouchableHighlight>
            </View>
        )
    }
}

purii

Bind this in Constructor

You have to bind the instance this to the function. It is recommend to do this in the constructor.

class LoginPage extends Component {
constructor(props) {
    super(props);
    this.handleOpenURL = this.handleOpenURL.bind(this);
}

componentDidMount() {
    Linking.addEventListener('url', this.handleOpenURL);
}
componentWillUnmount() {
    Linking.removeEventListener('url', this.handleOpenURL);
}
handleOpenURL(event) {
    let code = event.slice(22,86);
    console.log(code);         
    this.props.actions.fetchToken(code);
}

 render() {

    return (
        <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
            <TouchableHighlight style={{backgroundColor: '#9b59b6', height: 70, padding: 20}} onPress={this.openAuth.bind(this)}>
                <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
                    <Text style={{color: 'white', fontSize: 16}}>Authenticate with Dribbble</Text>
                </View>
            </TouchableHighlight>
        </View>
    )
}

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to call a function in the render function?

From Java

How to Call a Function inside a Render in React/Jsx

From Dev

how to call function inside reactjs render method properly?

From Dev

how to call other functions in flat list render function?

From Dev

Symfony 2 Call a function(with temlate render) in a Controller

From Dev

React.js call recursive function in render

From Dev

laravel pagination : Call to a member function render() on array

From Dev

laravel pagination : Call to a member function render() on array

From Dev

React - call handler from var in render function

From Dev

javascript nested function call in reactjs render classname

From Dev

VueJS pass props to children in render function call

From Dev

react call a function from ReactDOM.render

From Dev

React Native call a function in a render method

From Dev

How to call a function with a function as parameter?

From Dev

How to call a function within a function?

From Dev

How to call a jquery function?

From Dev

how to call a certain function

From Dev

How to call function in ruby

From Dev

How to call a function on click

From Dev

How to call function in LLVM

From Dev

How to call a function in Pseudocode?

From Dev

How to use call function?

From Dev

How to call to function

From Dev

How to call a function by a pointer?

From Dev

How to call a function as an argument

From Dev

How to call a function in wordpress?

From Dev

How to call a jquery function?

From Dev

How to call a shell function

From Dev

How to call a function dynamically?