shahen94 / react-native-switch

Customisable switch component for RN
MIT License
296 stars 172 forks source link

Warning: Failed prop type: Invalid prop `activeTextStyle` of type `array` supplied to `Switch`, expected `object` #91

Open benomatis opened 3 years ago

benomatis commented 3 years ago

I know this warning is being shown as PropTypes of style props are set to object and I used an array (as is common in React Native to define styles), but the interesting thing is that the styles I applied this way would still work.

I first thought I'd just submit a PR and turn this...

https://github.com/shahen94/react-native-switch/blob/23e4de68122c87b387e8dc72ab420a84483ae609/lib/Switch.js#L27

...into this...

activeTextStyle: PropTypes.oneOfType([
  PropTypes.object,
  PropTypes.array
]),

...but I was wondering if it would actually cause issues as it's being used like this further down:

https://github.com/shahen94/react-native-switch/blob/23e4de68122c87b387e8dc72ab420a84483ae609/lib/Switch.js#L228-L230

benomatis commented 3 years ago

As a workaround, I am now transforming the style array into an object:

// the styles array
const activeTextStyles = [
  { foo: 'bar' },
  condition && { baz: 'qux' }
]

// a simple function
const stylesArrayToObject = styles => Object.assign({}, ...styles)

// use it like this
<Switch
  // ...
  activeTextStyle={stylesArrayToObject(activeTextStyles)}
  // ...
/>

// or forget the function and just do...
<Switch
  // ...
  activeTextStyle={Object.assign({}, ...[
    { foo: 'bar' },
    condition && { baz: 'qux' }
  ])}
  // ...
/>