Is there any easy way how can I render React component into App.js but based on result from if condition? I have clock app which is supposed to 'ring' when alarm value which I define is equal to current time. This 'ring' effect I want to be that another component ,for example Ring.js will render when current time is equal to alarm time and it will contain text from state a I can simply unmount it by clicking a button.
Here Is my function with condition which is called every second to check if there's value in alarmHours state or not.
function checkTime(){
if(time.alarmHours && time.alarmMinutes){
if(time.currentHour === time.alarmHours && time.currentMinute === time.alarmMinutes &&
time.currentSecond === '00'){
//VALUES ARE EQUAL ,ALARM IS RINGING,RENDER COMPONENT
}
}
}
My first idea was to create empty variable ,and if the condition is fulfilled just simply insert Ring.js component into that variable.
let ring;
function checkTime(){
if(time.alarmHours && time.alarmMinutes){
if(time.currentHour === time.alarmHours && time.currentMinute === time.alarmMinutes &&
time.currentSecond === '00'){
ring = <Ring />
}
}
}
And then, simply render that variable in Return() of App.js ,like this.
Return(
<div>
{ring}
</div>
)
But it doesn't work a nothing is rendered.
I've tried to look in official React documentation ,they have there few examples but I don't really get how to implement that approach in my case. I don't know how to render that component if that condition is fulfilled. Can anybody please help me? I'll appreciate that. Thank you.
Link for codesandbox : https://codesandbox.io/s/rough-paper-xv0wi?file=/src/App.js
const [ringing, setRinging] = useState(false);. Further down in your JSX, you use{ringing && <Ring />}. Finally, when the alarm time is reached, you callsetRinging(true);