JavaScript/react/条件渲染:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
第20行: 第20行:
function render() {
function render() {
   return condition && <Component />;
   return condition && <Component />;
}</syntaxhighlight>
<div class="pageBoxGreen">某些场景下 <code>condition = false</code> 是,会将 false 以字符串形式输出到页面中。</div>
== IIFE ==
<syntaxhighlight lang="javascript">
function render() {
  return <>
    {
      (() => (condition ? <Component /> : null))();
    }
  </>;
}</syntaxhighlight>
}</syntaxhighlight>

2022年6月8日 (三) 11:11的版本

引言

IF/ELSE

function render() {
  if (condition) return <Component />;
  else return null;
}

三目运算符

function render() {
  return condition ? <Component /> : null;
}

&&

function render() {
  return condition && <Component />;
}
某些场景下 condition = false 是,会将 false 以字符串形式输出到页面中。

IIFE

function render() {
  return <>
    {
      (() => (condition ? <Component /> : null))();
    }
  </>;
}