By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Difference in wrapper and condition within in a component
JavaScript

Javascript – Difference in wrapper and condition within in a component

Admin
Last updated: 2023/12/20 at 4:39 PM
Admin
Share
3 Min Read
Difference in wrapper and condition within in a component

Problem:

I have written this code in my React which is a wrapper component to condition show something

Contents
Problem:Solution:
const AccessControl = ({ content, children }: acProps) => {
  return content ? children : null;
};

<AccessControl content={order.location.messageInternal}>
  <Grid md={12} className="result-three-block padding" p={0.1}>
    <p>{order.location.messageInternal}</p>
  </Grid>
</AccessControl>

I can also do in this way

{order.location.messageInternal && <Grid md={12} className="result-three-block padding" p={0.1}>
                  <p>{order.location.messageInternal}</p>
                </Grid>}

This is all happening inside a map function which is loading with 20 limit data.
So is it any performance issue in using wrapper component or with conditions within a component.

Solution:

Let’s show your two snippets to Babel (v. 7.23.2; targets: defaults, not ie 11, not ie_mob 11; presets: react/typescript).

The first one is transpiled into the same function and a _jsx() call:

const AccessControl = ({ content, children }) => {
  return content ? children : null;
};
/*#__PURE__*/ _jsx(AccessControl, {
  content: order.location.messageInternal,
  children: /*#__PURE__*/ _jsx(Grid, {
    md: 12,
    className: "result-three-block padding",
    p: 0.1,
    children: /*#__PURE__*/ _jsx("p", {
      children: order.location.messageInternal
    })
  })
});

The second also has a _jsx() call (note that I wrapped it in a Fragment, so only the inner one counts):

/*#__PURE__*/ _jsx(_Fragment, {
  children:
    order.location.messageInternal &&
    /*#__PURE__*/ _jsx(Grid, {
      md: 12,
      className: "result-three-block padding",
      p: 0.1,
      children: /*#__PURE__*/ _jsx("p", {
        children: order.location.messageInternal
      })
    })
});

Due to JS’s short circuitry behaviour, it will skip _jsx(Grid, /* ... */) as soon as order.location.messageInternal is determined as falsy. This is quite clear, even in the original source code.

Meanwhile, the first snippet evaluates the property values, both order.location.messageInternal and _jsx(Grid, /* ... */), before passing them to the outer _jsx():

/*#__PURE__*/ _jsx(AccessControl, {  // Step 3: Pass the object to the outer call
  content: order.location.messageInternal,  // Step 1: Evaluate order.location.messageInternal
  children: /*#__PURE__*/ _jsx(Grid, { /* ... */ })  // Step 2: Call _jsx(...)
});

Statically speaking, the second is potentially faster than the first. However, the difference in performance might not even worth noticing if your app doesn’t do hundreds to thousands of these updates every second or so. See also this video on the topic of premature optimization by CodeAesthetic.

Readability-wise, the second is worse, although the first is not really clear either. If you want some general suggestions on your code, check out our sister site Code Review.

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article How to display a nested array in Next.js or to change it to object Javascript – How to display a nested array in Next.js or to change it to object
Next Article What is the correct usage of next.js app and pages directories? [closed] Javascript – What is the correct usage of next.js app and pages directories? [closed]
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?