I understand that, within your React component, it's always good to render a component rather than use a function that renders some JSX. What I mean is,
Doing this :
export default function App() {
const [count, setCount] = useState(0);
const someRenderFunction = () => <p>Hey</p>;
return (
<div className="App">
{someRenderFunction()}
<button
onClick={() => {
setCount((c) => c + 1);
}}
>
Increment{" "}
</button>
<p>{count}</p>
</div>
);
}
is NOT encouraged. The render function should be exported into it's own Component, like this :
const HeyComponent = () => <p>Hey</p>
export default function App() {
const [count, setCount] = useState(0);
return (
<div className="App">
<HeyComponent />
<button
onClick={() => {
setCount((c) => c + 1);
}}
>
Increment{" "}
</button>
<p>{count}</p>
</div>
);
}
But I never really understood the benefits of this refactor. I tried placing checking the re-render behaviours, unmount behaviours. Still didn't see any difference. Can anyone explain in detail why this refactor is necessary/beneficial ?
In react, render is a method that tell react what to display. return in a method or function is the output of the method or function. Render is a method that tell react what to display. "The render method returns a description of what the DOM should look like, and then React efficiently updates the real DOM to match."
One of the advantages of JSX is that React creates a virtual DOM (a virtual representation of the page) to track changes and updates. Instead of rewriting the entire HTML, React modifies the DOM of the page whenever the information is updated. This is one of the main issues React was created to solve.
JSX is not a requirement for using React. Using React without JSX is especially convenient when you don't want to set up compilation in your build environment. Each JSX element is just syntactic sugar for calling React.
Nothing is better, because both have pros and cons. But class components are important to understand React flow and lifecycle methods. The new learner should practice React using class components. Once they are familiar with class components, they can learn and use functional components.
This is all about components
, so components should be reusable and should follow the DRY
principle, in your case that seems to be so simple and just as you said will prevent the someRenderFunction()
to be re-rendered if there aren't any changes to that component in the virtual dom
so the best practices are to use <X />
syntax always or for some cases const Y = <X />
also is more readable. testing is another reason to create more components and make them more decoupled. imagine here you need to pass props
to your someRenderFunction()
so you will lose the jsx feature for passing props as <X prop={PROP}/>
.
The actual difference between the two is that the function returning JSX is not a component, so it does not have its own state. When you use useState
in the function, the state change will cause the App
to be re-rendered.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With