Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
React-Native changing <Image /> source with state

I'm trying to create a React component that toggles a logo between black and white when pressed. I want to incorporate more functionality in the component down the road, but changeLogo() console.log will not even show in the debugger.

Any help / tips appreciated!

react-native: 0.39.2 react: ^15.4.2

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

export default class TestButton extends Component {
  constructor(props) {
    super(props);
    this.state = { uri: require('./icons/logo_white.png') }
  }

  changeLogo() {
    console.log('state changed!');
    this.setState({
      uri: require('./icons/logo_black.png')
    });
  }

  render() {
    return (
      <View
        style={styles.container}
      >
        <Image
          source={this.state.uri}
          style={styles.logoStyle}
          onPress={this.changeLogo}
        />
      </View>
    );
  }
}

const styles = {
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'blue',
  },
  logoStyle: {
    width: 200,
    height: 200,
    marginLeft: 10,
    marginRight: 5,
    alignSelf: 'center',
  },
};
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

onPress prop is not available for Image component. You need to wrap it with TouchableHighlight component like this:

import { View, Image, TouchableHighlight } from 'react-native';

...

<TouchableHighlight onPress={() => this.changeLogo()}>
  <Image
    source={this.state.uri}
    style={styles.logoStyle}
  />
</TouchableHighlight> 
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda