Right now I am using the react-native-elements component library for my app. Specifically I am using their Button component, which has a default color of grey applied to it.
How can I set a custom default color for these buttons without having to pass in style props every time?
Is there a simple function/method I can call, or do I have to look at creating a custom component to wrap it? I would prefer the former.
To change background color on click in React:Set the onClick prop on the element. When the element is clicked, set the active state. Use a ternary operator to conditionally set the background color based on the state variable.
To create custom buttons, you need to customize the <TouchableOpacity /> component and include the <Text /> component inside of it to display the button text. const AppButton = ({ onPress, title }) => ( <TouchableOpacity onPress={onPress} style={styles. appButtonContainer}> <Text style={styles.
You should use the hex code backgroundColor="#FF0000" instead of color="red" .
I know this is an old question, but since i found this post, i will leave this answer here for others to see.
react-native-elements supports theming since Oct 2018. As per the documentation you can use a theme provider in your RN application and override the library's default colors by doing something like this:
import { ThemeProvider } from 'react-native-elements';
const theme = {
colors: {
primary: 'pink',
}
}
....
....
render(){
...
return(
<ThemeProvider theme={theme} >
<App/>
</ThemeProvider>
)
}
The above example will change the primary color for all your components. By applying the same logic, you can change the background color only for button elements. You can also use theming for other customizations, like default component properties etc. For more information, check out the docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With