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

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
 
(未显示同一用户的4个中间版本)
第72行: 第72行:
   };
   };
}</syntaxhighlight>
}</syntaxhighlight>
== do {} ==
== switch ==
<syntaxhighlight lang="javascript">
function render() {
  return <>
    {
      (()=> {
        switch (condition) {
          case 1: return <Component1 />;
          case 2: return <Component2 />;
          default: null;
        }
      })()
    }
  </>;
}</syntaxhighlight>
== Enums objects ==
<syntaxhighlight lang="javascript">
const Components = Object.freeze({
  save: <Component1 />,
  edit: <Component2 />
});
function render() {
  const action = condition ? "save" : "edit":
  return <>
    {Components(action))}
  </>;
}
</syntaxhighlight>
== 参考 ==
* [https://blog.logrocket.com/conditional-rendering-in-react-c6b0e5af381e/#ifelse Conditional rendering in React: 9 methods with examples]