farmcommand2
farmcommand2

Reputation: 1486

my function is called and what is inside it is ran, but then i get `undefined` on the console

I am learning react native. For the code below:

import React, { Component } from 'react';
import { View, Text } from 'react-native';

export default class App extends Component {
  highlight() {
    console.log(111111111);
  }
  render() {
    console.log(222222);
    console.log(this.highlight());
    console.log(333333);
    return (
      <View>
        <Text>sdsd</Text>
      </View>
    );
  }
}

I get this output at the console:

2222222
111111111
undefined
33333333

and sdsd on the device screen. what is that undefined for?? if it is undefined how it ran the log inside the function?

Upvotes: 0

Views: 20

Answers (2)

Carcigenicate
Carcigenicate

Reputation: 45736

console.log(this.highlight());

Prints out the return of this.highlight(), but this.highlight() doesn't return anything.

Either return the value from this.highlight() and print it at the call site, or just write this.highlight().

Upvotes: 1

Juan
Juan

Reputation: 5589

I think in render() you are wanting to log what highlight() returns. But highlight doesn't return anything it does its own logging.

So render should look like this:

render() {
    console.log(222222);
    this.highlight();
    console.log(333333);
    return (
      <View>
        <Text>sdsd</Text>
      </View>
    );
  }

Upvotes: 1

Related Questions