Require React-Native conditionally

I want to create a generic element which returns div in the web and View in React-Native. Currently, it looks like this:

export const Element = ({children, ...rest}) => {
  if (typeof document != 'undefined') {
    return <div {...rest}>{children}</div>
  } 
  try {
    const View  = require('react-native').View;
    return <View {...rest}>{children}</View>
  } catch (e) {
    return {};
  }
};

I use try-catch because I get this error:

Module not found: Error: Can't resolve 'react-native'

How can I conditionally require React-Native without the need for try-catch?

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

React Native - Image Cache