G
G
gsdev992020-02-18 15:31:32
React
gsdev99, 2020-02-18 15:31:32

How to do dynamic rendering?

There is a component that dynamically renders components by type. Now it happens like this: one under the other. What is the right thing to do in this case, if I need the Title component (all elements) in section.
To get something like this:

<main>
    <section>
        <Title />
        <Title />
        <Title />
    </section>

    <New />
</main>

The data structure cannot be changed.
const Factory = ({ widget }) => {
  const widgetType = widget.type.toLowerCase()
  
  const Component = components[widgetType]
  
  if (widgetType === 'title') {
    // я так понимаю, вот здесь должна быть логика
  }
  
  return (
    <Component widget={widget} />
  )
}

https://codepen.io/gsdev99/pen/bGdeWbw

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2020-02-18
@gsdev99

What is the right way to do this...

The data structure cannot be changed.

"Correct" - no way. It is not clear where section should come from.
So the data structure will still have to be changed .

A
Alexander Epikhin, 2020-02-18
@leshiple

If I understand you correctly, then this is how you can do it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question