Recursive Blocks #1275
-
I'm basically building a page builder of all my react components and loving it so far. I iterate all my react components, and create a 'block' of each one. However the only problem is react components allow children, and I'd love for each block to have its own field which has additional blocks (which are components of that same library). However, then I would enter this weird recursive case where I continously iterate and hit no stopping point. Does anyone of a solution for how to accomplish such a thing? Does my problem make sense? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
HELL yes I have a solution for you. You need to import all of your React block components only at the top level where the first "blocks" are rendered, and then pass them through each additional layer of blocks as either props or with context. But the trick is to only import them once, at the top level, and then needle them through the rest of the way. So say you have a We had to do this internally in Payload with our field components themselves, because fields can render other fields and then boom - recursion. It's a common problem with programming in general! |
Beta Was this translation helpful? Give feedback.
-
I was talking about the React side. But if this is an issue in your Payload config and that is what you are referring to, the same principles would apply there too. |
Beta Was this translation helpful? Give feedback.
HELL yes I have a solution for you.
You need to import all of your React block components only at the top level where the first "blocks" are rendered, and then pass them through each additional layer of blocks as either props or with context. But the trick is to only import them once, at the top level, and then needle them through the rest of the way. So say you have a
RenderBlocks
component or similar. THAT component needs to accept your block components as props or retrieve them via context.We had to do this internally in Payload with our field components themselves, because fields can render other fields and then boom - recursion. It's a common problem with programming in general!