te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - What is the best way to render component by condition in React? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the best way to render component by condition in React? - Stack Overflow

programmeradmin3浏览0评论

I have a ponent that needs to render by condition, I'm confused between the two ways like the code below and I am figuring out the remended way of React. What is the best way to set render conditions for a ponent? I'm new to React and would love some help.

The first way:

const MyComponent1 = () => {
  return (
    <div>This is my ponent 1</div>
  )
}

{isRender && <MyComponent />}

The second way:

const MyComponent2 = ({ isRender }) => {
  if (!isRender) return null;

  return (
    <div>This is my ponent 2</div>
  )
}

<MyComponent isRender/>

I feel the second approach will redundancy the logic code inside this ponent if the ponent is not rendered (e.g: hooks, handle function, ...) The first way will not affect the CPU because the ponent is not rendered so the internal logic will not be executed. Is that true?

I have a ponent that needs to render by condition, I'm confused between the two ways like the code below and I am figuring out the remended way of React. What is the best way to set render conditions for a ponent? I'm new to React and would love some help.

The first way:

const MyComponent1 = () => {
  return (
    <div>This is my ponent 1</div>
  )
}

{isRender && <MyComponent />}

The second way:

const MyComponent2 = ({ isRender }) => {
  if (!isRender) return null;

  return (
    <div>This is my ponent 2</div>
  )
}

<MyComponent isRender/>

I feel the second approach will redundancy the logic code inside this ponent if the ponent is not rendered (e.g: hooks, handle function, ...) The first way will not affect the CPU because the ponent is not rendered so the internal logic will not be executed. Is that true?

Share Improve this question edited Dec 21, 2024 at 10:39 Mayank Kumar Chaudhari 18.6k13 gold badges67 silver badges151 bronze badges asked Jul 21, 2023 at 5:32 EppleEpple 9721 gold badge12 silver badges33 bronze badges 2
  • second approach is better, it will not break your app later – Medet Tleukabiluly Commented Jul 21, 2023 at 5:34
  • @MedetTleukabiluly Second approach is not remended because a ponent returning null is pretty unexpected and bad practice as it makes things harder to debug. A ponent should always render something and the conditional rendering logic should remain in the parent. – xyres Commented Jul 21, 2024 at 4:54
Add a ment  | 

2 Answers 2

Reset to default 7

The first way is much better. As you rightly pointed out, you need to keep all hooks before the first return statement. You will be unnecessarily adding those hooks and any other logic when you don't need to render the ponent.

Secondly, and more importantly, while using the first way you can use lazy loading to only load the ponent when it needs to be rendered for the first time and thereby improving the performance.

among the most mon strategies are:

Employing the if/else or ternary operator:

To conditionally render ponents, you can use either a standard if/else expression or a ternary operator. Here is an illustration of the ternary operator in use:

function MyComponent({ isLoggedIn }) {

 return (
    <div>
      {isLoggedIn ? <LoggedInComponent /> : <LoggedOutComponent />}
    </div>
  );
}

Using && operator: The && operator can be used for simple conditional rendering when you want to render a ponent based on a single condition:

function MyComponent({ isLoading }) {
  return (
    <div>
      {isLoading && <LoadingComponent />}
    </div>
  );
}

Using switch/case statement: If you have multiple conditions to handle, you can use a switch statement:

function MyComponent({ status }) {
  switch (status) {
    case 'success':
      return <SuccessComponent />;
    case 'error':
      return <ErrorComponent />;
    default:
      return <DefaultComponent />;
  }
}

Using a function for conditional rendering: You can define a separate function to decide which ponent to render:

function MyComponent({ condition }) {
  function getComponent() {
    if (condition === 'A') {
      return <ComponentA />;
    } else if (condition === 'B') {
      return <ComponentB />;
    } else {
      return <DefaultComponent />;
    }
  }

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

Select a strategy based on your unique use case and coding preferences. Utilising tools like React Router or Redux in some circumstances could also offer more effective solutions to manage plex conditional rendering scenarios. When selecting a method, always keep readability and maintainability of your code in mind.

发布评论

评论列表(0)

  1. 暂无评论