0

I have a pretty interesting question.

Can you get a full React Element through an API call?

Example of API response:

<View style={{backgroundColor: 'red'}}>
   <Text>
      Hello
   </Hello>
</View>

Can this response be saved in a variable and used in the final return?

poPaTheGuru
  • 1,009
  • 1
  • 13
  • 35

1 Answers1

0

You can use the data you get from an API call to create a function that returns a component like this:

import * as React from 'react';
import { Text, StyleSheet, View } from 'react-native';

export default function App() {

  const TestComponent = ({ words }) => {
    return (
      <Text>{words}</Text>
    );
  }

  return (
    <View>
      <TestComponent words={"Hello World"}/>
    </View>
  );
}
man517
  • 419
  • 5
  • 9
  • Yes, I totally agree with you. But my question is if my API returns a full JSX like: Hello. Somehow writing that I realise that my API will return this as String and can't really transform it to a React element. Is it true? – poPaTheGuru Oct 27 '21 at 08:04
  • 1
    Try taking a look at this: https://stackoverflow.com/questions/51108640/how-to-convert-string-to-jsx-in-react-native – man517 Oct 27 '21 at 17:05
  • thank you for the topic, sure it's a start – poPaTheGuru Oct 27 '21 at 19:14