Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

Change image url using ternary operator when a condition is met

$
0
0

When the user shakes the phone I want to change an image, and let it stay rendered that way. With this I can change the image but it goes back once the condition is not fulfiled:

function Shaker(){useEffect(() => {        const subscription = accelerometer.subscribe(({ x, y, z }) =>            setSensorData({ x, y, z })            );  }, []);const acceleration = Math.sqrt(sensorData.x * sensorData.x + sensorData.y * sensorData.y + sensorData.z * sensorData.z);const imgchange = acceleration > 12 ? imgThatIwantToRenderAfterConditionAndStay.png : 'DefaultImg.png' ;return (<View><Image style={{ width: 100, height: 100 }} source={{ uri: imgchange }} /></View>}

When I created a boolean and passed it instead of acceleration > 12 i got read only error:

const accelerationobtained = false;if (acceleration > 12) { accelerationobtained = true; }const ifimg = accelerationobtained ? imguri : 'https://www.nicepng.com/png/full/416-4167329_shot-glass.png' ;

I think my solution is dumb and I didnt expect it to work so I want to get educated on how should I handle such case.


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>