1

I recently made a pull request at my company and got feedback on some code that I had written and I wanted some other opinions on this. We have an component called Icon that can take another component as a prop like so:

<Icon component={ArrowDown}/>

this simply renders the following:

<IconContainer>
    <ArrowDown/>
</IconContainer>

Now you can also do the follow if you need to create a custom icon:

<Icon component={()=><div>custom Icon</div>}/>

The reviewer commented that the function ()=><div>custom Icon</div> should be removed outside the scope for performance reasons to prevent re-rendering:

const CustomIcon = ()=><div>custom Icon</div>
const someComponent = ()=><Icon component={customIcon}/>

I'm not convinced that this will improve performance (code-readability sure) but wanted to get some other opinions on it. Thanks!

1
  • I believe the component prop is re-created on every render, which causes the Ìcon to re-render. But I'm not sure that would affect the performance. Because simply it is a small component and not a layout with so many nested divs. Commented Jun 10, 2021 at 9:44

1 Answer 1

4

Arrow functions are anonymous and will be re-instantiated on every render.

If you create a named component, it will have a reference and will not be re-rendered by React unless and until required (through state update).

And also, as you mentioned, it provides better readability and an option for code splitting.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.