PudgePacket PudgePacket - 3 months ago 7
React JSX Question

Rendering platform agnostic wrapper in React

In a library I'm writing, there is a component which adds props to it's children then renders them. Because React only renders 1 element, the children have to be wrapped in something. Up until trying to port the library to React Native it was wrapping in a

div
. That doesn't work in React Native however.

Is there a platform agnostic wrapper that will automatically choose the correct one depending on platform? Or is there a way to write logic that will choose between
div
and
View
at runtime?

I don't want to write one for web and one for native with the only difference being
div
swapped for
View
...

Answer

View maps directly to the native view equivalent on whatever platform React is running on, whether that is a UIView, <div>, android.view, etc.

https://facebook.github.io/react-native/releases/0.27/docs/view.html