Solucionado (ver solução)
Solucionado
(ver solução)
4
respostas

render()

Olá, eu ainda não entendi muito bem porque o const width , ou seja quando pegamos a largura através da Dimensions , fica fora do render e da classe e quando criamos as const fotos colocamos dentro do render.

Alguem poderia me explicar o porque ? Obrigado !

4 respostas

Olá, consegue postar o código aí pra eu analisar e tentar te ajudar!?

Att, Eduardo Ribeiro da Silva

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  View,
  Image,
  Dimensions,
  FlatList
} from 'react-native';

const width = Dimensions.get('screen').width;

export default class Post extends Component {

  render() {
    const fotos = []
    return (
      <View>
        <View style={styles.cabecalho}>
          <Image source={{uri: this.props.foto.urlPerfil}}
              style={styles.fotoDePerfil}/>
          <Text>{this.props.foto.loginUsuario}</Text>
        </View>
        <Image source={{uri: this.props.foto.urlFoto}}
            style={styles.foto}/>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  cabecalho: {
    margin: 10,
    flexDirection: 'row',
    alignItems: 'center'
  },
  fotoDePerfil: {
    marginRight: 10,
    borderRadius: 20,
    width: 40,
    height: 40
  },
  foto: {
    width: width,
    height: width
  }
});

Eu coloquei uma const fotos ali apenas para ilustrar o que eu estava perguntar.

solução!

Segundo a documentação (https://facebook.github.io/react-native/docs/dimensions.html) é desaconselhável fazer "cache" (como o código usado no curso), devido as dimensões poderem ser dinamicamente alteradas, como por exemplo, ao rotacionar o device. Veja:

Initial dimensions are set before runApplication is called so they should be available before any other require's are run, but may be updated later.

Note: Although dimensions are available immediately, they may change (**e.g due to device rotation**) so any rendering logic or styles that depend on these constants should try to call this function on every render, rather than caching the value (for example, using inline styles rather than setting a value in a StyleSheet).

Neste caso acredito ser mais correto que o width seja obtido dentro do render().

Att, Eduardo Ribeiro da Silva

Obrigado Eduardo !

abraço

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software