Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!