In my application I want to only display a component if two conditions are true. From the parent, this is what it looks like:
{!online ? (
<TouchableOpacity
onPress={changeOnlineStatus}
>
<Text>
Go online
</Text>
</TouchableOpacity>
) : online && queue === 0 ? (
<TouchableOpacity
onPress={changeOnlineStatus}
>
<Text>
Go offline
</Text>
</TouchableOpacity>
) : (
<Service
details={userDetails.queueItems[0].details}
/>
)}
Here, changeOnlineStatus
status simply changes the online
variable so that the two TouchableOpacities switch. The problem is the Service component. It should only be rendered if the queue is populated, but the queue exists as an array in a constants file. In the Service component's file, the queue is changed by a timer with the following states/hooks:
// timer value of 5
const [timeRemaining, setTimeRemaining] = useState(5);
// timer counts down to 0 and then removes the first element of the array
useEffect(() => {
setTimeout(() => {
if (timeRemaining > 0) {
setTimeRemaining(timeRemaining - 1);
} else {
userDetails.queueItems.shift();
return;
}
}, 1000);
}, [timeRemaining]);
So when the time runs out, the array has no elements, but the Service component stays rendered. Why isn't it removed since the condition (a populated array) is no longer true?
If it's important, the queue exists in a constants file and looks like this:
queueItems: [
{
title: "Test",
},
]