简体   繁体   English

如何根据 If 条件的结果渲染 React 组件?

[英]How to render React Component depending on result of If condition?

Is there any easy way how can I render React component into App.js but based on result from if condition?有什么简单的方法可以根据 if 条件的结果将 React 组件渲染到 App.js 中吗? 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.这个“环”效果我想成为另一个组件,例如 Ring.js 将在当前时间等于警报时间时呈现,它将包含来自 state 的文本,我可以通过单击按钮简单地卸载它。

Here Is my function with condition which is called every second to check if there's value in alarmHours state or not.这是我的 function 条件,每秒调用一次以检查 alarmHours state 中是否存在值。

  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.我的第一个想法是创建空变量,如果满足条件,只需将 Ring.js 组件插入该变量即可。

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.然后,只需在 App.js 的 Return() 中渲染该变量,就像这样。

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.我尝试查看官方 React 文档,他们的示例很少,但我并没有真正了解如何在我的案例中实现该方法。 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代码沙盒链接: https://codesandbox.io/s/rough-paper-xv0wi?file=/src/App.js

Try this:尝试这个:

let ring = <Ring />;

function checkTime(){
  if(time.alarmHours && time.alarmMinutes){
    if(time.currentHour === time.alarmHours && time.currentMinute === time.alarmMinutes && 
    time.currentSecond === '00'){
      return true;
    }
  }
   return false;
}
...

return (
  <div>
   {checktime() ? ring : null}
  </div>
)

I would update your checkTime() function to return <Ring /> if it's ready, otherwise return null and then you can just:我会更新你的checkTime() function 以返回<Ring />如果它准备好了,否则返回 null 然后你可以:

return (
  <div>
    {checkTime()}
  </div>
);

ie IE

function checkTime(){
  if(time.alarmHours && time.alarmMinutes){
    if(time.currentHour === time.alarmHours && time.currentMinute === time.alarmMinutes && 
    time.currentSecond === '00'){
      return <Ring />
    }
  }

  return null;
}

ring is no longer needed.不再需要ring

EDIT: additional question in comments编辑:评论中的附加问题

if you want Ring to show when the alarm time has been reached, I would change the pattern and introduce a new boolean state value like const [showRing, setShowRing] = React.useState(false) which is initially set to false.如果您希望Ring在到达闹钟时间时显示,我会更改模式并引入一个新的 boolean state 值,如const [showRing, setShowRing] = React.useState(false)最初设置为 false。 checkTime() would then no longer return any JSX and just also call setShowRing(true) and then you would:然后checkTime()将不再返回任何 JSX 并且只调用setShowRing(true)然后你会:

return (
  <div>
    {showRing && <Ring />}
  </div>
);

ie IE

function checkTime(){
  if(time.alarmHours && time.alarmMinutes){
    if(time.currentHour === time.alarmHours && time.currentMinute === time.alarmMinutes && 
    time.currentSecond === '00'){
      setShowRing(true);
    }
  }
}

And wherever you want to remove the <Ring /> you would call setShowRing(false)无论你想删除<Ring />你都会调用setShowRing(false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM