React Native Blur Background
This is an Example to Make a Blur Background in React Native. To Make a Blur Background in React Native we will use the BlurView component from @react-native-community/blur provided by react-native-community. This is very useful if you want to make a splash screen.
In this example, we will make a simple screen with a Switch to switch between simple and blurred screens and three buttons to switch different types of blur effects. So let’s get started.
Make BlurView using
<BlurView
viewRef={this.state.viewRef}
style={styles.blurView}
blurRadius={1}
blurType={blurType} //xlight, light, dark
// The following props are also available on Android:
// blurRadius={20}
// downsampleFactor={10}
// overlayColor={'rgba(0, 0, 255, .6)'} // set a blue overlay
/>
Different Types of Blur Effects
- xlight: Extra Light Blur Type
- light: Light Blur Type
- dark: Dark Blur Type
- extraDark: Extra Dark Blur Type(tvOS only)
- regular: Regular Blur Type (iOS 10+ and tvOS only)
- prominent: Prominent Blur Type (iOS 10+ and tvOS only)
Adjust Blur Intensity
0-100 – Adjusts Blur Intensity
Note: The maximum blur amount on Android is 32, so higher values will be clamped to 32.
To Make a React Native App
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react native command line interface to make our React Native App.
If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:
npm uninstall -g react-native-cli @react-native-community/cli
Run the following commands to create a new React Native project
npx react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the --version argument:
npx react-native init ProjectName --version X.XX.X
Note If the above command is failing, you may have old version of react-native
or react-native-cli
installed globally on your pc. Try uninstalling the cli and run the cli using npx.
This will make a project structure with an index file named App.js in your project directory.
Installation of Dependency
To use BlurView
we need to install @react-native-community/blur
dependency.
To install this open the terminal and jump into your project using
cd ProjectName
Run the following command to install
npm install @react-native-community/blur --save
This command will copy all the dependencies into your node_module directory. –save is optional, it is just to update the @react-native-community/blur dependency in your package.json file.
CocoaPods Installation
Please use the following command to install CocoaPods
npx pod-install
Code
Now Open App.js in any code editor and replace the code with the following code
App.js
// Make a Blur Background in React Native
// https://aboutreact.com/blur-background/
// import React in our code
import React, {useState, createRef} from 'react';
// import all the components we are going to use
import {
Image,
InteractionManager,
StyleSheet,
Switch,
Text,
View,
TouchableOpacity,
findNodeHandle,
} from 'react-native';
//import BlurView to make the Blur Background
import {BlurView} from '@react-native-community/blur';
const App = () => {
const [showBlur, setShowBlur] = useState(true);
const [viewRef, setViewRef] = useState(null);
const [blurType, setBlurType] = useState('light');
const backgroundImageRef = createRef();
const tintColor = ['#ffffff', '#000000'];
if (blurType === 'xlight') {
tintColor.reverse();
}
const renderBlurView = () => {
//Function for the blur background
return (
<View
style={{
flexDirection: 'column',
justifyContent: 'flex-end'
}}>
{viewRef && (
<BlurView
viewRef={viewRef}
style={styles.blurViewStyle}
blurRadius={1}
blurType={blurType}
// Additional available on Android
// blurRadius={20}
// downsampleFactor={10}
// overlayColor={'rgba(0, 0, 255, .6)'}
/>
)}
<View
style={{
flex: 1,
flexDirection: 'column',
justifyContent: 'flex-end',
paddingBottom: 32,
}}>
<Text
style={[
styles.textStyle,
{color: tintColor[0]
}]}>
Blur component
</Text>
<TouchableOpacity
style={styles.buttonStyle}
onPress={() => {
//To make extra light background
setBlurType('xlight');
}}>
<Text
style={[
styles.textStyle,
{color: tintColor[0]}
]}>
xlight
</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.buttonStyle}
onPress={() => {
//To make light background
setBlurType('light');
}}>
<Text
style={[
styles.textStyle,
{color: tintColor[0]}
]}>
light
</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.buttonStyle}
onPress={() => {
//To make dark light background
setBlurType('dark');
}}>
<Text
style={[
styles.textStyle,
{color: tintColor[0]}
]}>
dark
</Text>
</TouchableOpacity>
</View>
</View>
);
};
return (
<View style={styles.container}>
<Image
source={{
uri:
'https://raw.githubusercontent.com/AboutReact/sampleresource/master/site_banner_vertical.png',
}}
//source={require('./bgimage.jpg')}
style={styles.imageStyle}
ref={backgroundImageRef}
onLoadEnd={() => {
// Workaround for a tricky race condition on initial load
InteractionManager.runAfterInteractions(() => {
setTimeout(() => {
setViewRef(
findNodeHandle(backgroundImageRef.current)
);
}, 500);
});
}}
/>
{showBlur ? renderBlurView() : null}
<View style={styles.blurToggleStyle}>
<Text
style={[
styles.textStyle,
{color: tintColor[0]}
]}>
Show Blur Background
</Text>
<Text
style={[
styles.textStyle,
{color: tintColor[0]}
]}>
{showBlur ? 'Yes' : 'No'}
</Text>
<Switch
onValueChange={
(value) => setShowBlur(value)
}
value={showBlur}
/>
</View>
</View>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
imageStyle: {
position: 'absolute',
left: 0,
top: 0,
bottom: 0,
right: 0,
resizeMode: 'cover',
width: null,
height: null,
},
blurViewStyle: {
position: 'absolute',
left: 0,
top: 0,
bottom: 0,
right: 0,
},
textStyle: {
fontSize: 22,
fontWeight: 'bold',
textAlign: 'center',
margin: 10,
color: '#d0d0d0',
},
blurToggleStyle: {
position: 'absolute',
top: 30,
alignItems: 'center',
},
buttonStyle: {
alignItems: 'center',
backgroundColor: 'lightgreen',
width: 300,
marginLeft: 100,
marginRight: 100,
marginTop: 16,
},
});
To Run the React Native App
Open the terminal again and jump into your project using.
cd ProjectName
1. Start Metro Bundler
First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command
npx react-native start
Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.
2. Start React Native Application
To run the project on an Android Virtual Device or on real debugging device
npx react-native run-android
or on the iOS Simulator by running (macOS only)
npx react-native run-ios
Output Screenshots
IOS
Android
This is how you can Make a Blur Background in React Native. If you have any doubts or want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!
Hope you liked it. 🙂