0

This code is requiring a constructor. I'm not understanding the need to have a constructor to use the 'this' (Eu não estou entendendo a necessidade de ter um construtor para usar o 'this')

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


class Botao extends Component{

this.styles = StyleSheet.create({}); // requiring a constructor

render(){
    return(
        <TouchableOpacity>
            <View>
                <Text>Clique</Text>
            </View>
        </TouchableOpacity>
    );
}
  }

can I do it this way without using it?

class Botao extends Component{

render(){
    return(
       <TouchableOpacity>
            <View>
                <Text style={this.styles.texto}>Clique</Text>
            </View>
       </TouchableOpacity>
    );
}

styles = StyleSheet.create({
    texto:{
        fontSize: 60
    }
}); 

}
1
  • 1
    Yes, the second way is how to do it. Declaring styles like that makes it a class property which is accessed from elsewhere in the class with this Commented Jun 6, 2019 at 15:24

1 Answer 1

6

You actually have two options here...

1) Use the class constructor:

class Botao extends Component{

  constructor(){
   this.styles = StyleSheet.create({}); // requiring a constructor
  }

  render(){
    return(
        <TouchableOpacity>
            <View>
                <Text>Clique</Text>
            </View>
        </TouchableOpacity>
    );
  }
}

2) Declare it directly as a class property (without this):

class Botao extends Component{

  styles = StyleSheet.create({});

  render(){
    return(
        <TouchableOpacity>
            <View>
                <Text>Clique</Text>
            </View>
        </TouchableOpacity>
    );
  }
}

In both cases, you would access styles from elsewhere in your class using this.styles

If you're writing a React component, you usually don't need to use the constructor method. As it says in the docs:

If you don’t initialize state and you don’t bind methods, you don’t need to implement a constructor for your React component.

Sign up to request clarification or add additional context in comments.

6 Comments

what is the role of the constructor in the first case?
Note that the second option uses syntax which is not yet a part of standard javascript, and thus requires babel's plugin-proposal-class-properties to use. React applications commonly use this plugin. Writing the code the second way gets transpiled into the first way.
without the constructor, 'this' does not assume the global scope?
@wesif I've updated my answer with some more info - in short, it serves no useful purpose. this has global scope in both cases, but if you choose to attach your styles property in the constructor, you have to use this in order to attach it to the containing class.
Before construction, there is no instance, and so this has no meaning. Does that help?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.