Gradients bring out a colorful taste in our apps. So it’s important to know how and when to use them in your mobile apps. In React Native, gradients are extremely easy to integrate, and in this article, we are going to describe exactly how to implement them. There are mainly two types of linear gradients in React Native and CSS: Linear and Radial gradient

Two of the most used JavaScript libraries for generating gradients in React Native are

They are both very similar and for this article on gradients in React Native we’re going to make use of expo-linear-gradient.


For Expo: expo install expo-linear-gradient

For bare projects: 

  • Install and configure unimodules
  • expo install expo-linear-gradient
  • cd ios && pod install

First use case of gradients are to make buttons look solid:

The color prop accepts an array of colors to be displayed in a linear gradient.

Let’s make another nice thing with gradients: We are going to combine an image with a gradient, like the example below:

Now, what’s going on here? We’re overlaying a gradient on an ImageBackground, a React Native component and then reduce the opacity of the gradient. Then we center our the text inside the LinearGradient.

I introduced a prop start, the start prop is an array to tell the gradient where to start. The first element positions the gradient horizontally(x-axis), while the second element positions the gradient vertically.

For example, [0.1, 0.2] means that the gradient will start 10% from the left and 20% from the top.

Let us see some visual examples:

This is what a gradient with start [0.9, 0.9] looks like.

LinearGradient in React Native is easy and seamless to implement. We have used them to make our buttons have a solid look and feel. We have made a crisp UI background for our text and looked into the start prop.

This is how you can implement gradients in React Native. As you can see, it was pretty straightforward.

Source link

Write A Comment