ReactJS Tips & Tricks: Avoid Nested Render Functions

A common thing I noticed in a lot of projects I worked on is the Nested Render Functions approach to render UI elements.

Let’s dive into this approach and how to change in a better way.

What are Nested Render Functions?

Basically, it is wh…


This content originally appeared on DEV Community and was authored by Helder Burato Berto

A common thing I noticed in a lot of projects I worked on is the Nested Render Functions approach to render UI elements.

Let's dive into this approach and how to change in a better way.

What are Nested Render Functions?

Basically, it is when you declare a part of UI render in a function inside of a component, such as:

const Component = () => {
  function renderSection() {
    return <section>This is my section.</section>
  }

  return (
    <div>
      {renderSection()}
      ...
    </div>
  )
}

Since components are just functions, it is the same as declaring new components inside the current Component.

Extracting to a New Component

It is much better to extract to a new component, it will help you to create unit tests more easily and isolated to the component.

Let's re-create the example I mentioned before, like the following:

const Section = () => <section>This is my section.</section>

const Component = () => (
  <div>
    <Section />
    ...
  </div>
)

Instead of using closures, now you have a pure function for Section component, that's more readable and easy to give their props.

Wrapping Up

With this approach, you will create more deterministic components taking the benefit of React pure component.

It will help you to test the component and create isolated behaviour for every component.


This content originally appeared on DEV Community and was authored by Helder Burato Berto


Print Share Comment Cite Upload Translate Updates
APA

Helder Burato Berto | Sciencx (2021-11-10T09:22:45+00:00) ReactJS Tips & Tricks: Avoid Nested Render Functions. Retrieved from https://www.scien.cx/2021/11/10/reactjs-tips-tricks-avoid-nested-render-functions/

MLA
" » ReactJS Tips & Tricks: Avoid Nested Render Functions." Helder Burato Berto | Sciencx - Wednesday November 10, 2021, https://www.scien.cx/2021/11/10/reactjs-tips-tricks-avoid-nested-render-functions/
HARVARD
Helder Burato Berto | Sciencx Wednesday November 10, 2021 » ReactJS Tips & Tricks: Avoid Nested Render Functions., viewed ,<https://www.scien.cx/2021/11/10/reactjs-tips-tricks-avoid-nested-render-functions/>
VANCOUVER
Helder Burato Berto | Sciencx - » ReactJS Tips & Tricks: Avoid Nested Render Functions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/10/reactjs-tips-tricks-avoid-nested-render-functions/
CHICAGO
" » ReactJS Tips & Tricks: Avoid Nested Render Functions." Helder Burato Berto | Sciencx - Accessed . https://www.scien.cx/2021/11/10/reactjs-tips-tricks-avoid-nested-render-functions/
IEEE
" » ReactJS Tips & Tricks: Avoid Nested Render Functions." Helder Burato Berto | Sciencx [Online]. Available: https://www.scien.cx/2021/11/10/reactjs-tips-tricks-avoid-nested-render-functions/. [Accessed: ]
rf:citation
» ReactJS Tips & Tricks: Avoid Nested Render Functions | Helder Burato Berto | Sciencx | https://www.scien.cx/2021/11/10/reactjs-tips-tricks-avoid-nested-render-functions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.