02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Creating Dynamic JSX Tags

In this tutorial, we’ll explore how to create dynamic JSX tags to make React components more flexible and compliant with external frameworks like Bootstrap. By the end, you’ll understand how to conditionally render different HTML elements, such as <a> and <button>, using advanced ES6 techniques.


Why Use Dynamic JSX Tags?

Dynamic JSX tags are a key tool for enhancing reusability in React components. By conditionally rendering tags, you can align with framework documentation, like Bootstrap’s, while accommodating varying user requirements. This approach simplifies your components, improves maintainability, and ensures compliance with HTML standards.


Step 1: Understanding the Problem

Bootstrap recommends using different tags based on the context:

  • Use <a> for links or navigation.
  • Use <button> for actions like form submissions.

Hardcoding one tag limits flexibility. Instead, we’ll create a dynamic solution to switch between these tags based on properties (props) passed to the component.


Step 2: Creating the Dynamic Component

  1. Define a Variable for the Tag
    Use a ternary operator to decide whether to render an <a> or a <button> based on the presence of an href property.

    const tag = this.props.href ? 'a' : 'button';
    
  2. Render the Dynamic Tag
    JSX allows rendering variables as tags when written with uppercase first letters. Use the tag variable to decide the rendered element dynamically.

    return <Tag>{this.props.children}</Tag>;
    
  3. Ensure Correct Attributes
    Pass all relevant props to the dynamic tag using JSX spread syntax:

    <Tag {...this.props}>{this.props.children}</Tag>;
    

Step 3: Testing the Component

Test your component by rendering it in different scenarios:

  • When an href is provided, the component should render as an <a> tag.
  • Without an href, it should render as a <button> tag.

Example Usage:

<Button href="https://02geek.com">Visit 02Geek</Button>
<Button>Submit</Button>

Output in DOM:

  • The first button will render as an <a> with a link to https://02geek.com.
  • The second button will render as a <button> with default behavior.

Step 4: Debugging and Fine-Tuning

If you encounter errors like unsupported attributes, review the props being passed to the component. Using the JSX spread syntax (...this.props) ensures all valid props are applied, but invalid attributes might still cause warnings.


Conclusion

Dynamic JSX tags empower developers to create more adaptable and reusable React components. By tailoring components to specific contexts while maintaining simplicity, you’re set to build robust, framework-compliant UI elements.

In the next tutorial, we’ll dive deeper into JSX spread syntax and refine our components for even greater flexibility.

Video Transcript

00:00In the last lecture, our focus was
00:02about reusability really.
00:04And when we're talking about reusability, we really need
00:07to make our content more dynamic, which is part
00:09of this lecture as we talk about how to create dynamic JSX.
00:14And really what we're gonna do is we're gonna basically try
00:16to match up to the bootstrap documentation
00:19as we're creating a button based on that documentation,
00:22which you're encouraged to look up into that
00:25documentation yourself as well, where we're going
00:27to basically need to have a button
00:29or an ATAC really, just depending on the very various usages
00:34of the user itself.
00:35I'm gonna take a look a deeper look into that right away.
00:39Alright? So something that I encourage you
00:41to do is get more familiar with the rules
00:44of CSS in Bootstrap four,
00:47moreover than that specifically the button,
00:49because that's our main hero in this lecture.
00:52So with that said, I'm just gonna give you a bit
00:54of a background of this button.
00:57Now, generally speaking, buttons in React are encouraged
01:01to use the actual button tag that exists in HTML.
01:06But in reality, whenever we're gonna just put inside a link,
01:09we really just want to use a regular a tag.
01:13But in any other situ agents, such as a submit button, such
01:16as any other button that is not actually just a regular link
01:19that we're adding some JavaScript functionality on top
01:22of it, we would really want to use an actual button.
01:26So what I'm gonna do is I'm gonna show you some really cool
01:28stuff that you could do with JSX.
01:30And for one, what I wanna do is I'm gonna create
01:33here a new variable.
01:34And really it doesn't have to be a variable,
01:36it could be a let or even a constant.
01:39So as a constant, once we define the variable, that
01:43specific variable is not going to change.
01:45And that's really all that it implies.
01:48Now I'm gonna go ahead and just give it a name.
01:49So I'm gonna call this tag
01:51and I'm gonna set it to have a Valium
01:53and I'm gonna use here something
01:55that is called ary operators.
01:58And in this ary operator,
02:00I'm gonna check if this props has an hre F.
02:03Now what this actually does is basically an if condition.
02:07If the value here is anything but false, zero null
02:11or zero or nothing
02:13or any value that is not positive,
02:15then it will basically return an L in any other situation,
02:19it will do the true whatever logic is here
02:23and anywhere else it'll do the false.
02:24Now it's uh, it's not new to ES six false,
02:29it's not new to E ES six,
02:30but it's probably not as commonly known by developers.
02:34So really what I'm asking here is if this HR exists,
02:38I'm gonna go ahead and say, okay, this is an A tag.
02:41If this HRF does not exist, I'm gonna go ahead
02:44and say, okay, you could be a button.
02:47And now that I've done that,
02:48and by the way, notice how I used here in uppercase T.
02:52And that's really important
02:53because in JSX,
02:55the only time it actually does dynamic things is if it finds
02:58Out that the tag above is an upper case.
03:01Now in this case, I'm gonna go ahead
03:03and set here our tag to be our opening and closing.
03:06And although this seems mighty confusing and mighty weird,
03:10but what actually is gonna go on,
03:11it's gonna actually check when it's gonna re-render.
03:13And I'm gonna go ahead and inspect that element.
03:17We'll see that the element itself, if we find it,
03:20let me just open up our a better inspector.
03:24And if we go ahead into our diviv of React, we will find
03:28inside of, uh, all this good fun stuff our container.
03:32And in that container we're gonna find the last paragraph.
03:35And in it we'll find two buttons at our ATT tags
03:38and notice what's gonna happen as soon as I remove out
03:41of our client out, out of our app.
03:44In the second item, I'm not gonna have an hrif.
03:47Now once that Hrif is gone, if I go ahead
03:50and save this, it will automatically recognize it
03:53and it's gonna set up a button instead of an A tag.
03:57In this lecture, we took our button to be even closer
04:00to the official documentation
04:01to make our button even more reusable.
04:04In the next lecture, we're gonna understand the JSX spread
04:07more in depth so we can make our component even more
04:10dynamic.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!