change button color react native

I think it is definitely better to use TouchableOpacity instead of Button tag as Button is creating styling discrepancies in Android and iOS platforms .

You can use the code below and it looks very similar to a button and it acts like one:

 <TouchableOpacity
         style={styles.button}
         onPress={this.onPress}
       >
         <Text> Touch Here </Text>
 </TouchableOpacity>

const styles = StyleSheet.create({
  button: {
    alignItems: 'center',
    backgroundColor: '#DDDDDD',
    padding: 10
  }
})

The react Button component renders the native button on each platform it uses. Because of this, it does not respond to the style prop. It has its own set of props.

The correct way to use it would have been

<Button color="#ff5c5c" title="I'm a button!" />

You can see the documentation at https://facebook.github.io/react-native/docs/button.html

Now, say you do want to make super customizable button, for that you'll have to use views and touchable opacity. Something along the lines of this.

<TouchableOpacity onPress={...}>
  {... button markup}
</TouchableOpacity>

You'll wrap that up in your own button component and use it.


Yes button does not respond to styles. But alternative solution is that we can use react-native-element component.

First install the packages which is mentioned below

npm install react-native-elements npm i react-native-linear-gradient npm i react-native-vector-icons

And then import the packages to your code

    import { Button } from 'react-native-elements';
    import LinearGradient from 'react-native-linear-gradient';
    import Icon from 'react-native-vector-icons/FontAwesome';

    <Button ViewComponent={LinearGradient} // Don't forget this!
    linearGradientProps={{
    colors: ['#ffffff','blue', 'grey'],
    start: { x: 0, y: 0.5 },
    end: { x: 1, y: 0.5 },
    }} onPress={()=> this.props.navigation.navigate('First')} title="Click me"/>

For more info here is the link: https://react-native-elements.github.io/react-native-elements/docs/button.html

Tags:

Native

Reactjs