EPeak Daily

Easy methods to create a background video in React Native – freeCodeCamp.org

0 12

Peleton dwelling display screen clone

On this put up, we’re going to create a backgroundVideo in React Native. If in case you have simply began with React Native take a look at my article What it’s essential know to begin constructing cellular apps with React Native.

Background movies can add a pleasant impact to the UI of an app. They might be useful additionally if you wish to show, for instance, adverts or ship a message to the person, like we’ll do right here.

style="display:block; text-align:center;" data-ad-format="fluid" data-ad-layout="in-article" data-ad-client="ca-pub-4791668236379065" data-ad-slot="8840547438">

You will want some fundamental necessities. To get began, you need to have the react-native surroundings setup. Meaning you’ve gotten:

  • react-native-cli put in
  • Android SDK; you probably have a mac you gained’t want that, simply Xcode

Getting began

First issues first, let’s bootstrap a brand new React Native app. In my case I’m utilizing react-native-cli. So in your terminal run:

react-native init myapp

This could set up all of the dependencies and packages to run your React Native app.

Subsequent step is to run and set up the app on the simulator.

For iOS:

react-native run-ios

This could open up the iOS simulator.

On Android:

react-native run-android 

You might have some hassle with Android. I like to recommend that you just use Genymotion and the Android emulator or take a look at this pleasant information to arrange the surroundings.

First what we’re going to do is clone the Peleton app’s dwelling display screen. We’re utilizing react-native-video for video streaming, and styled-component for styling. So it’s important to set up them:

yarn add react-native-video styled-components
npm -i react-native-video styled-components --save

Then it’s essential hyperlink react-native-video as a result of it accommodates native code — and for styled-components we don’t want that. So merely run:

react-native hyperlink

You don’t have to fret in regards to the different issues, simply give attention to the Video Part. First, import Video from react-native-video and begin utilizing it!

import import Video from "react-native-video";

Let’s break it down:

  • supply: the trail to the supply video. You need to use the URL as an alternative:
  • model: the costume model we wish to give to the video, and the important thing to creating the background video
  • resizeMode: in our case it’s cowl; you possibly can attempt additionally include or stretch however this gained’t give us what we wish

And different props are non-obligatory.

Let’s transfer to the necessary half: inserting the video within the background place. Let’s outline the types.

// We use StyleSheet from react-native so remember to import it
//import {StyleSheet} from "react-native";
const { peak } = Dimensions.get("window");
const types = StyleSheet.create({
backgroundVideo: {
peak: peak,
place: "absolute",
high: 0,
left: 0,
alignItems: "stretch",
backside: 0,
proper: 0

What did we do right here?

We gave the video a place :absolute and we give it the window peak of the system. We used the Dimensions from React Native to make sure that the video is taking over the entire hight — high:0, left:0,backside:0,proper:0 — in order that the video takes up all of the house!

The complete code:

Additionally, you can also make this element reusable by doing the next:


And you need to use it with different elements:

That’s just about it. Thanks for studying!

Picture by David Boca on Unsplash

My earlier put up about React Native:

Different posts:

Yow will discover me on Twitter 🐦

Subscribe to my Mailing checklist to remain tuned for upcoming articles!

Supply hyperlink

Leave A Reply

Hey there!

Sign in

Forgot password?

Processing files…