Overview
Congrats! You've installed React Native Elements and your immediate question goes something like this:
So umm, how do I change how it looks?
Great question! A UI Kit wouldn't be that useful if the apps everyone built looked the same right? For this case React Native Elements provide a number of props on each component to enable you to style them how you want.
Colors
Setting styles in the theme is as simple as using the name of the component, as a key and the props you want to change as the value.
import { ThemeProvider, createTheme } from '@rneui/themed';
const theme = createTheme({
lightColors: {
primary: '#e7e7e8',
},
darkColors: {
primary: '#000',
},
mode: 'light',
});
const App = () => {
return <ThemeProvider theme={theme}>{/* ... */}</ThemeProvider>;
};
Using the respective platform's native colors
You may want to style your app using the native color palette. You can do this
using the colors
object and the Platform
API.
import { Platform } from 'react-native';
import { Button, lightColors, createTheme, ThemeProvider } from '@rneui/themed';
const theme = createTheme({
lightColors: {
...Platform.select({
default: lightColors.platform.android,
ios: lightColors.platform.ios,
}),
},
});
const App = () => {
return (
<ThemeProvider theme={theme}>
{/* This button's color will now be the default iOS / Android blue. */}
<Button title="My Button" />
</ThemeProvider>
);
};
Component Styles
Every component from React Native Elements has a container around it. The
container is just a traditional <View />
from react native that has some
styling on it. This default styling prevents components from colliding with each
other. If you want to change how two components react to each on the screen your
first stop should be the containerStyle
prop.
Similar to containerStyle
, components may provide their custom style props
like buttonStyle
, titleStyle
etc. Always refer to the documentation for the
component to find out which style props it provides.
Theming Components
While component styles are great for single use, you may want to have the same styling for every instance of a component. For example, you may want all your buttons to be blue or have the same font. Here are some ways to reuse styles with React Native Elements.
Using Composition
With this approach, we create one component with the styles we want and use that instead of the built-in component.
import React from 'react';
import { Button } from '@rneui/themed';
const RaisedButton = (props) => <Button raised {...props} />;
// Your App
const App = () => {
return <RaisedButton title="Yea" />;
};
If we want to use a button that's raised in our app, we can use RaisedButton
instead of using Button
. This component still accepts all the props from the
normal Button
just that it has the raised
prop set by default.
Using ThemeProvider
The previous solution works great for only one component, but imagine having to do this for every component you want custom styles for. That could get a bit tedious to manage. Thankfully, there's a better way to do this.
import { ThemeProvider, Button, createTheme } from '@rneui/themed';
const theme = createTheme({
components: {
[themeKey]: {
// ... props
},
},
});
const App = () => {
return <ThemeProvider theme={theme}>{/* ... */}</ThemeProvider>;
};
If you do not specify theme
in ThemeProvider, it would use defaultTheme
Order of Styles
What happens now if we want a Button
that isn't raised? To do that we have to understand the order in which styles are applied.
Internal > Theme > External
Internal
Internal components styles are the styles which are defined in the component file. These are applied first.
Theme
Theme styles are the values that are set by the ThemeProvider If present, these are applied second.
import { ThemeProvider, Button, createTheme } from '@rneui/themed';
const theme = createTheme({
components: {
Button: {
titleStyle: {
color: 'red',
},
},
},
});
const App = () => {
return (
<ThemeProvider theme={theme}>
<Button title="My Button" />
</ThemeProvider>
);
};
This will override the white color for the title set in the component's style.
External
External styles are the styles which are set through the component props. These are applied last and have the highest precedence.
import { ThemeProvider, Button, createTheme } from '@rneui/themed';
const theme = createTheme({
components: {
Button: {
titleStyle: {
color: 'red',
},
},
},
});
const App = () => {
return (
<ThemeProvider theme={theme}>
<Button title="My Button" titleStyle={{ color: 'pink' }} />
</ThemeProvider>
);
};
This will override both the white color for the title set in the component's style as well as the red color set in the theme.
Remember if you want to override the values set in the theme you can always use component props.