Conditional Style?
How can we do conditional style with Nativebase component?
Here is my code.
<ListItem onPress={} style={[nativeBaseStyle.notSelected, this.isSelected() && nativeBaseStyle.isSelected]}>
<Text>{rowData.firstName}</Text></ListItem>
NativeBaseStyle looks like this.
export default{
isSelected:{
backgroundColor: 'blue',
},
notSelected:{
backgroundColor: 'transparent',
},
}
It works with React Native component but not NativeBase component.
react-native, react and native-base version
react-native 0.41.2 native-base 2.0.12
Expected behaviour
Different style should be loaded base on the condition
Actual behaviour
Gave me the warnings.
Warning: Failed prop type: Invalid prop 'style' of type 'array' supplied to 'Styled(ListItem)', expected 'object'.
Is the bug present in both ios and android or in any one of them?
Only Tested on Android
It doesn't work for me either. Looks like NativeBase doesn't support array style format. But you may use ES6:
style={{...nativeBaseStyle.notSelected, ...(this.isSelected() && nativeBaseStyle.isSelected)}}
Hey @ChiWuSage , We are planning to incorporate array style format in newer versions of NativeBase, for now you can use the ES6 as pointed by @remo-sk above. Thanks :)
#575
With the latest of NativeBase version 2.3.0. Now all components accepts the styling same way as any other Rn components which includes conditional styling
@shivrajkumar i'm trying to do ternary condition on ListItem and still throwing me a warning and not working. This is weird.
<ListItem
style={ this.state.locationService ? scopedStyles.disabledProperty : ''}>
...
Shows warning: Failed prop type: Invalid prop style supplied to Styled(ListItem)