Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Render Text Only Components in React 16

    Nik GrafNik Graf

    In this session we create a comment component to explore how to create components that only render text.

    In previous versions of we had to wrap our text in needless <span> or <div>, we will see how React 16 removes the unneeded structure.

    reactReact
    ^16.0.0
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Here, you can see a common component rendering some text. The comment's purpose is to modify the text, mainly replacing text smileys with smile emojis. It currently contains a wrapping SPAN. Such wrapping elements were needed for components in React 15 or lower.

    Let's explore a DOM structure. As you can see, our component is inaudible 0:24 wrapped in a DIV, then once more in a SPAN. While the SPAN doesn't cause any problems, it also doesn't add any value, and the whole DOM would be cleaner and a bit more lightweight without it.

    React 16 makes this possible. We can update our common component to return just plain text. Let's refresh the page, and as you can see, the comment will still be rendered. When we inspect the DOM, you can see the unnecessary SPAN is gone.

    Discuss

    Discuss