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.
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, components may provide their custom style props
titleStyle etc. Always refer to the documentation for the
component to find out which style props it provides.
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.
With this approach, we create one component with the styles we want and use that instead of the built-in component.
If we want to use a button that's raised in our app, we can use
instead of using
Button. This component still accepts all the props from the
Button just that it has the
raised prop set by default.
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. React Native Elements ships with a 3 utilities for large-scale theming.
Firstly you'll want to set up your
The example above achieves the same goals as the first example — apply the same
styles to multiple instances of
Button in the app. However this example
raised prop to every instance of
Button inside the component
ThemeProvider. Both of these buttons will have the
set to true.
This is extremely convenient and is made possible through React's Context API.
TypeScript Definitions (extending the default theme)
TypeScript definitions for your theme can be extended by using TypeScript's declaration merging feature. First you need to create a declaration file called
react-native-elements.d.ts and then declare the module
react-native-elements and 're-export' the types that you want to extend.
i.e. below we add a custom p1Style to the Text theme object and we add a bunch of colors to the colors object.
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 components styles are the styles which are defined in the component file. These are applied first.
Theme styles are the values that are set by the ThemeProvider If present, these are applied second.
This will override the white color for the title set in the component's style.
External styles are the styles which are set through the component props. These are applied last and have the highest precendence.
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.
Note: To theme subcomponents such as
ListItem.Title, in your theme remove the dot and list them as "ListItemTitle"
The Theme Object
By default, the theme object looks like this. You can add whatever values you want to the theme, and they will be merged with the default. By default the platform colors aren't used anywhere. These native colors are added for your convenience.
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.
React Native Elements also provides a preset dark mode palette to get you started with using dark mode in your app.
Use the prop
ThemeProvider to set the default dark theme. You may want to set this by using a button,
or by using the user's configured settings
Using the theme in your own components
You may want to make use of the theming utilities in your own components. For
this you can use the
withTheme HOC exported from this library. It adds three
props to the component it wraps -
updateTheme function merges the theme passed in with the current theme.
replaceTheme function merges the theme passed in with the default theme.
Don't want to wrap your components? You can use the
which uses render props!
You can also use the ThemeContext directly if you use hooks.
Using the respective platform's native colors
You may want to style your app using the native color palette. You can do this
colors object and the
This section outlines some common pitfalls when using Theming.
My local styles aren't working with the theme
It's important to understand that the
ThemeProvider works by merging your local(external) styles with those set on the theme.
This means that in both cases the type of these styles must be the same.
In both cases the style is an
In both cases the style is an
🚫 Doesn't work
In one case the style is an
objectand another the style is an