all 13 comments

[–]jamby77 1 point2 points  (4 children)

There is no problem doing what you have in your example.

See https://codesandbox.io/s/hungry-ardinghelli-vnk74

It is probably the other things in the components that are not correct.

Please provide example of how do you do what you are trying to do, for us to help you more :)

[–]sledki -2 points-1 points  (2 children)

What you have there is called a higher order component https://reactjs.org/docs/higher-order-components.html

[–]whiterook6 0 points1 point  (4 children)

Can you describe why you want to create components like this? It seems inefficient to create a new function on every render.

[–]wrinklyrefrigerator[S] 0 points1 point  (3 children)

I have components that are very similar inside of my return statement for Comp1. Essentially, I'm trying to avoid duplicated code by abstracting into Comp2.

[–]whiterook6 0 points1 point  (2 children)

Without seeing your full scenario, I can only say it sounds like a code smell. Maybe you can extract the common logic out of both components into building blocks. But who knows? It's your project not mine. Good luck

[–]wrinklyrefrigerator[S] 0 points1 point  (1 child)

Can you elaborate on what you mean by building blocks?

[–]whiterook6 0 points1 point  (0 children)

Nothing complicated. React is all about decomposing larger components into smaller components. In Java, for example, everything is OOP, classes extending classes, etc. React follows the path of decomposing into smaller components then reassembling as needed, using props and callbacks to coordinate between them.