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

131
Views
React Native - How to switch between several images depending on 2 states?

I am coding an alarm application right now and I can't seem to figure out how to display the desired image. There are two states:

  1. switchValue : the alarm is enabled(off) or disabled
  2. alarmValue : the alarm is on or off (triggered or not)

Right now only switchValue is displayed and it works depending on true/false. But how can I switch between images depending on switchValue AND alarmValue AND undefined if the alarm is not connected?

I get 'switchValue' and 'alarmValue' through redux toolkit.

Here's my code:

import { useDispatch, useSelector } from 'react-redux';

import { toggleOn, toggleOff } from '../components/switch';
import { alarmOn, alarmOff} from '../components/alarm';

function Home({navigation}) {

   const imageAlarmOff = require('../images/Alarm_off.png'); // enabled but not triggered
   const imageAlarmOn = require('../images/Alarm_on.png');  // enabled and triggered
   const imageAlarmDisabled = require('../images/Alarm_off_disabled.png');  // disabled
   const imageAlarmUndefined = require('../images/Alarm_undefined.png');  // not connected

   const switchValue = useSelector((state) => state.switch.active);
   const alarmValue = useSelector((state) => state.alarm.active);
   const dispatch = useDispatch();


   return (
      <View style={globalStyles.containerBodyHome}>

        <View style={globalStyles.containerMainHome}>
          <ImageBackground 
            style={globalStyles.imageHome} 
            source={switchValue ? imageAlarmOff : imageAlarmDisabled}
          >
          </ImageBackground>
        </View>

      </View>
   );
}


export default Home;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If I understand correctly you would like to manage multiple images?

You can use switch statement and base on switchValue return appropriate image.

function Home({navigation}) {

   const imageAlarmOff = require('../images/Alarm_off.png'); // enabled but not triggered
   const imageAlarmOn = require('../images/Alarm_on.png');  // enabled and triggered
   const imageAlarmDisabled = require('../images/Alarm_off_disabled.png');  // disabled
   const imageAlarmUndefined = require('../images/Alarm_undefined.png');  // not connected

   const switchValue = useSelector((state) => state.switch.active);
   const alarmValue = useSelector((state) => state.alarm.active);
   const dispatch = useDispatch();

   const handleImage = () => {
    switch(switchValue) {
      case true: {
        if(alarmValue) return imageAlarmOn
        return imageAlarmOff
      }
      case false: {
       return imageAlarmDisabled
      }
     default: {
      return imageAlarmUndefined
     }}}


   return (
      <View style={globalStyles.containerBodyHome}>

        <View style={globalStyles.containerMainHome}>
          <ImageBackground 
            style={globalStyles.imageHome} 
            source={handleImage()}
          >
          </ImageBackground>
        </View>

      </View>
   );
}
about 4 years ago · Juan Pablo Isaza 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!