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.