react-native-switch icon indicating copy to clipboard operation
react-native-switch copied to clipboard

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

Open benomatis opened this issue 4 years ago • 1 comments

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 avatar Mar 11 '21 17:03 benomatis

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' }
  ])}
  // ...
/>

benomatis avatar Mar 11 '21 20:03 benomatis