gpt4 book ai didi

javascript - Reactjs - 显示组件的多个条件 - if/else block

转载 作者:行者123 更新时间:2023-11-28 04:30:46 27 4
gpt4 key购买 nike

我是reactjs新手,希望将旧的js文件转换为reactjs组件。我已经写出了组件,现在尝试附加相同/相似的 if/else 逻辑结构

我知道如果这样的逻辑你可以做到

return (
<div>
{ true && <AddAccount /> }
{ false && <AccountAdded /> }
</div>
);

https://facebook.github.io/react/docs/conditional-rendering.html

那么代码应该看起来像这样 - 有很多内联样式条件运算符吗?

  return (
<div>
{isLoggedIn ? (
<LogoutButton onClick={this.handleLogoutClick} />
) : (
<LoginButton onClick={this.handleLoginClick} />
)}
</div>
);

//旧js

{if isset($interview)}

{* First Information Section *}
{* ******************************************** *}

{assign var="path" value="/views/video/video_first_section.tpl" }
{include file="$base_path$path"}


<div class="row show-for-small-only" style="height: 50px;"></div>

{* Interview Tips Section *}
{* ******************************************** *}

{assign var="path" value="/views/video/video_interview_tips.tpl" }
{include file="$base_path$path"}

{* Conference Calls Section *}
{* ******************************************** *}

{assign var="path" value="/views/video/video_call.tpl" }
{include file="$base_path$path"}


{if $user_type eq 'professional'}

{* after interview section - professional *}
{* ******************************************** *}

{assign var="path" value="/views/video/video_after_interview_professional.tpl" }
{include file="$base_path$path"}

{elseif $user_type eq 'employer'}

{* after interview section - employer *}
{* ******************************************** *}

{assign var="path" value="/views/video/video_after_interview_employer.tpl" }
{include file="$base_path$path"}

{/if}

{* Thank you page *}
{* ******************************************** *}

{assign var="path" value="/views/video/video_feedback_thank_you.tpl" }
{include file="$base_path$path"}

{else}

{* No Interview Page *}
{* ******************************************** *}

{assign var="path" value="/views/video/video_no_interview.tpl" }
{include file="$base_path$path"}

{/if}

//当前react js尝试

return (
<div>
{/* check if the user has an interview upcoming in the next 30 minutes */}
{/* if isset($interview) */}

{/* First Information Section */}
<VideoFirstSection lang={lang} />

<div className='row show-for-small-only' style={{height: '50px'}} />

{/* Interview Tips Section */}
<VideoInterviewTips lang={lang} />

{/* Conference Calls Section */}
<VideoCall lang={lang} />
{/* include file="$base_path$path" */}

{/* if $user_type eq 'professional' */}
{/* after interview section - professional */}
<VideoAfterInterviewProfessional lang={lang} />
{/* elseif $user_type eq 'employer' */}
{/* after interview section - employer */}
<VideoAfterInterviewEmployer lang={lang} />
{/* /if */}

{/* Thank you page */}
<VideoFeedbackThankYou lang={lang} />

{/* else */}
{/* No Interview Page */}
<VideoNoInterview lang={lang} />
{/* /if */}

</div>
)

//最新尝试 - 获取意外的 token 语言

{isset($interview) ? 
{/* First Information Section */}
<VideoFirstSection lang={lang} />

<div className='row show-for-small-only' style={{height: '50px'}} />

{/* Interview Tips Section */}
<VideoInterviewTips lang={lang} />

{/* Conference Calls Section */}
<VideoCall lang={lang} />

{($userType eq 'professional') ?
<VideoAfterInterviewProfessional lang={lang} />
: ($userType eq 'employer') ?
<VideoAfterInterviewEmployer lang={lang} />
}

{/* Thank you page */}
<VideoFeedbackThankYou lang={lang} />
: (
{/* No Interview Page */}
<VideoNoInterview lang={lang} />
}

最佳答案

我想说为 if 和 else 条件创建单独的组件,然后你可以渲染它们,它会更清晰

return (
<div>
{/* check if the user has an interview upcoming in the next 30 minutes */}
{/* if isset($interview) */}

{isset($interview)? <IfComponent/>: <ElseComponent/>}


</div>
)

如果组件

render () {
return (<div>
{/* First Information Section */}
<VideoFirstSection lang={lang} />

<div className='row show-for-small-only' style={{height: '50px'}} />

{/* Interview Tips Section */}
<VideoInterviewTips lang={lang} />

{/* Conference Calls Section */}
<VideoCall lang={lang} />
{/* include file="$base_path$path" */}

{($user_type eq 'professional')? <VideoAfterInterviewProfessional lang={lang} />: ($user_type eq 'employer')? <VideoAfterInterviewEmployer lang={lang} />: null }


{/* Thank you page */}
<VideoFeedbackThankYou lang={lang} /></div>
)

}

其他组件

render() {
return (
<div>
{/* No Interview Page */}
<VideoNoInterview lang={lang} />
</div>
)
}

关于javascript - Reactjs - 显示组件的多个条件 - if/else block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44628920/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com