Skip to content

Issue with bundler while using Platform.select in release build #17852

Description

@varungupta85

Is this a bug report?

Yes

Have you read the Contributing Guidelines?

Yes

Environment

Environment:
OS: macOS Sierra 10.12.6
Node: 8.2.1
Yarn: 1.1.0
npm: 5.3.0
Watchman: 4.9.0
Xcode: Xcode 9.2 Build version 9C40b
Android Studio: 2.3 AI-162.4069837

Packages: (wanted => installed)
react: ^16.2.0 => 16.2.0
react-native: ^0.52.2 => 0.52.2

Target Platform: iOS and Android

Steps to Reproduce

I have a react-native app in production for the last few months. I updated to latest react-native 0.52.2 and started to see some strange problems with Platform.select behaviour only in the release app. Values set using Platform.select were not set in certain cases. I was able to confirm it by updating the release app to print logs. I want to emphasise again that it happens only in release app and NOT in development app. Below are some code excerpts that work and don't work.

// I have a reusable module to show a context menu.
// It exposes an API `ContextMenu.show(options)` that can be used to show the context menu.
// The items in the context menu could be different on iOS vs Android. 
// Below piece of code used to work before I updated to 0.52.2 but it doesn't work now.

let ctxOptions = {
  items: Platform.select({
    'ios': showDelete ? [I18n.t('takePhoto'), I18n.t('chooseFromLibrary'), I18n.t('delete'), I18n.t('cancel')] :
      [I18n.t('takePhoto'), I18n.t('chooseFromLibrary'), I18n.t('cancel')],
    'android': showDelete ? [I18n.t('takePhoto'), I18n.t('chooseFromLibrary'), I18n.t('delete')] :
      [I18n.t('takePhoto'), I18n.t('chooseFromLibrary')]
  }),
  info: Platform.select({
    'ios': {
      destructiveButtonIndex: showDelete ? 2 : undefined,
      cancelButtonIndex: showDelete ? 3 : 2
    },
    'android': {}
  })
}

// Both info and items are undefined on both iOS and Android
ContextMenu.show(ctxOptions)

// I changed the above code to not use Platform.select but just pick the right object based on Platform.OS value without changing anything else and this works

let ctxOptions = {
  items: ({
    'ios': showDelete ? [I18n.t('takePhoto'), I18n.t('chooseFromLibrary'), I18n.t('delete'), I18n.t('cancel')] :
      [I18n.t('takePhoto'), I18n.t('chooseFromLibrary'), I18n.t('cancel')],
    'android': showDelete ? [I18n.t('takePhoto'), I18n.t('chooseFromLibrary'), I18n.t('delete')] :
      [I18n.t('takePhoto'), I18n.t('chooseFromLibrary')]
  })[Platform.OS],
  info: ({
    'ios': {
      destructiveButtonIndex: showDelete ? 2 : undefined,
      cancelButtonIndex: showDelete ? 3 : 2
    },
    'android': {}
  })[Platform.OS]
}

// Both info and items are set according to the OS
ContextMenu.show(ctxOptions)

// I also changed the above statements to ternary statements using Platform.OS and that works as well.

I am using Platform.select at other places such as styles such as below where it works as expected.

const styles = StyleSheet.create({
  container: Platform.select({
    android: {
      flex: 1,
      justifyContent: 'center',
      alignItems: 'center',
      backgroundColor: 'rgba(0, 0, 0, 0.4)',
    },
    ios: {
      justifyContent: 'center',
      alignItems: 'center',
    },
  })
})

This leads me to believe that this may be an issue with the bundler used in the release app causing the values to be not set.

Expected Behavior

The values set using Platform.select should be set according to the OS

Actual Behavior

The values were not set as described above.

Reproducible Demo

I have created a new project which can be downloaded from here. In the App.js file, in componentDidMount, there are two calls: one with Platform.select and the other with Platform.OS as described above. The app will abort when Platform.select section is not commented out and wouldn't abort when Platform.OS section is not commented out only in the release builds.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Ran CommandsOne of our bots successfully processed a command.Resolution: LockedThis issue was locked by the bot.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions