D
D
Damir Balgabaev2020-03-06 11:00:40
JavaScript
Damir Balgabaev, 2020-03-06 11:00:40

How to dynamically render different components in React Native?

For example, I have a store with the following data:
Page: [
{type: 'Button', label: 'Ok'},
{type: 'Card', label: 'News', Image: 'request...'},
{ type: 'TextInput', placeholder: 'Hello'}
];

These components need to be dynamically rendered on the page. But for example flatList renders components of only one type. How can this be implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-03-06
@BalgabayevD

It's better to change the structure:

[
  {
    type: 'Button',
    props: {
      label: 'Ok'
    }
  },
  {
    type: 'Card',
    props: {
      label: 'News',
      Image: 'request...'
    }
  },
  {
    type: 'TextInput',
    props: {
      placeholder: 'Hello'
    }
  }
];

Then it will be possible to do this in the method render:
{{page.map(element => <element.type {...element.props} />)}}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question