How can i code the below shape in React Native? - react-native

Click to See the Image
Note: The curved space should be transparent.

use react-native-svg to get this shape
snack: https://snack.expo.io/#ashwith00/juicy-tortilla
here is the example:
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import { Svg, Path } from 'react-native-svg';
const SIZE = 250;
const RADVALUE = SIZE - 40;
export default function App() {
return (
<View style={styles.container}>
<View style={styles.traingleCorner}>
<Svg
width={`${SIZE}`}
height={`${SIZE}`}
fill="red"
strokeWidth={1}
viewBox={`0 0 ${SIZE} ${SIZE}`}>
<Path
d={`M0 0L ${SIZE} 0L${SIZE} ${SIZE}Q${RADVALUE} ${
SIZE - RADVALUE
} 0 0 `}
/>
</Svg>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
traingleCorner: {
width: SIZE,
height: SIZE,
},
coloredPart: {
flex: 1,
backgroundColor: 'red',
},
uncoloredPart: {
...StyleSheet.absoluteFillObject,
borderBottomLeftRadius: SIZE,
backgroundColor: '#ecf0f1',
},
});

Related

Text strings must be rendered within a <Text> component. When creating a button

I'm new to react native and trying to create a button, here is my StartScreen:
import React from 'react' import Background from '../components/Background' import AppButton from '../components/AppButton'
export default function StartScreen({ navigation }) {
return(
<Background>
<AppButton title="HEEEY!" size="sm" backgroundColor="#007bff" />;
</Background>
) }
Here is my AppButton:
import React from 'react'
import { StyleSheet, TouchableOpacity, Text } from "react-native";
export default function AppButton ({ onPress, title, size, backgroundColor }) {
return (
<TouchableOpacity
onPress={onPress}
style={[
styles.appButtonContainer,
size === "sm" && {
paddingHorizontal: 8,
paddingVertical: 6,
elevation: 6
},
backgroundColor && { backgroundColor }
]}
>
<Text style={[styles.appButtonText, size === "sm" && { fontSize: 14 }]}>
{title}
</Text>
</TouchableOpacity>
)
};
const styles = StyleSheet.create({
screenContainer: {
flex: 1,
justifyContent: "center",
padding: 16
},
appButtonContainer: {
elevation: 8,
backgroundColor: "#009688",
borderRadius: 10,
paddingVertical: 10,
paddingHorizontal: 12
},
appButtonText: {
fontSize: 18,
color: "#fff",
fontWeight: "bold",
alignSelf: "center",
textTransform: "uppercase"
}
});
Here is my Background:
import React from 'react'
import { ImageBackground, StyleSheet, KeyboardAvoidingView } from 'react-native'
import { theme } from '../core/theme'
export default function Background({ children }) {
return (
<ImageBackground style={styles.background}>
<KeyboardAvoidingView style={styles.container} behavior="padding">
{children}
</KeyboardAvoidingView>
</ImageBackground>
)
}
const styles = StyleSheet.create({
background: {
flex: 1,
width: '100%',
backgroundColor: theme.colors.surface,
},
container: {
flex: 1,
padding: 20,
width: '100%',
maxWidth: 340,
alignSelf: 'center',
alignItems: 'center',
justifyContent: 'center',
},
})
When I try to run the application in my Android phone I get the following error:
Error: Text strings must be rendered within a component.
Blockquote
But I cant figure out where this error happens. Can someone spot the mistake?
You have an unnecessary semicolon (;) in your StartScreen JSX, remove it.
export default function StartScreen({ navigation }) {
return(
<Background>
<AppButton title="HEEEY!" size="sm" backgroundColor="#007bff" />
</Background>
); }

React native web stylesheet.create styles not working correctly

I was trying to create a layout with an embedded react SVG aligned at the center, however, it is not aligning the items, as shown below.
import { StyleSheet, Text, TouchableHighlight, View } from 'react-native';
import { StackScreenProps } from '#react-navigation/stack';
import { UseAuth } from '../hooks/useAuth';
import { SvgXml } from '#cantoo/rn-svg';
import { enterPINSVG } from '../assets/svgs/enterPIN';
export default function SplashScreen({
navigation,
useAuthInstance,
}: {
navigation: StackScreenProps<any>;
useAuthInstance: UseAuth;
}) {
return (
<View style={styles.container}>
<View style={styles.logoWrapper}> <===== alignItems here doesnot work
<SvgXml
xml={enterPINSVG}
style={{
display: 'flex',
backgroundColor: 'pink',
}}
/>
</View>
<View style={styles.welcomeTextWrapper}>
<Text style={styles.welcomeText}>Welcome to</Text>
<Text style={styles.companyText}>ChapChap</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flexDirection: 'column',
flex: 1,
backgroundColor: '#DD1521',
},
logoWrapper: {
flex: 3,
backgroundColor: 'maroon',
alignItems: 'center',
justifyContent: 'center',
},
welcomeTextWrapper: {
flex: 1,
backgroundColor: '#DD1521',
alignItems: 'center',
display: 'flex',
},
welcomeText: {
color: '#fff',
fontSize: 22,
fontWeight: '600',
},
});
The SVG is exported from an assets js files as below
export const enterPINSVG = `<svg xmlns="http://www.w3.org/2000/svg" width="56.483" height="62.973" viewBox="0 0 56.483 62.973">
<g id="Component_166_2" data-name="Component 166 – 2" transform="translate(0.539 0.673)">
<path id="Icon_map-sheild" data-name="Icon map-sheild" d="M53.844,21.757A26.591,26.591,0,0,1,57.825,9.67L48.977,1.08a16.183,16.183,0,0,1-9.585,3.858A16.932,16.932,0,0,1,30,3.106a18.133,18.133,0,0,1-9.4,1.832,17.326,17.326,0,0,1-9.123-3.5l-8.87,8.587A26.031,26.031,0,0,1,6.245,21.757c.113,1.88-.439,4.471-1.681,7.816q-.845,2.37-1.476,4.809a23.573,23.573,0,0,0-.569,3.235,15.508,15.508,0,0,0,3.1,9.647,36,36,0,0,0,8.157,6.921,53.762,53.762,0,0,0,9.581,3.936l1.861.841c.585.27,1.211.537,1.864.825A7.746,7.746,0,0,1,30,62.415a8.434,8.434,0,0,1,3-2.628q1.21-.5,2.4-1.056l1.4-.609c.48-.229,1.106-.494,1.868-.787q1.414-.544,2.843-1.048a23.875,23.875,0,0,0,4.787-2.1,41.2,41.2,0,0,0,8.057-6.823,15.281,15.281,0,0,0,3.2-9.742,34.163,34.163,0,0,0-2.131-7.783c-1.227-3.459-1.77-6.143-1.592-8.08Z" transform="translate(-2.519 -1.08)" fill="none" stroke="#000" stroke-width="1"/>
<path id="Icon_ionic-ios-lock" data-name="Icon ionic-ios-lock" d="M24.376,12.44H22.865V9.5a6.043,6.043,0,1,0-12.086-.082V12.44H9.268A2.525,2.525,0,0,0,6.75,14.958V27.044a2.525,2.525,0,0,0,2.518,2.518H24.376a2.525,2.525,0,0,0,2.518-2.518V14.958A2.525,2.525,0,0,0,24.376,12.44ZM17.7,20.3v4.331a.9.9,0,0,1-.837.906.882.882,0,0,1-.925-.881V20.3a2.014,2.014,0,1,1,1.763,0Zm3.4-7.856H12.541V9.418a4.281,4.281,0,0,1,8.561,0Z" transform="translate(11.25 13.626)" fill="none" stroke="#000" stroke-width="1"/>
</g>
</svg>`
However when I use an inline style like below it works,
import { StyleSheet, Text, TouchableHighlight, View } from 'react-native';
import { StackScreenProps } from '#react-navigation/stack';
import { UseAuth } from '../hooks/useAuth';
import { SvgXml } from '#cantoo/rn-svg';
import { enterPINSVG } from '../assets/svgs/enterPIN';
export default function SplashScreen({
navigation,
useAuthInstance,
}: {
navigation: StackScreenProps<any>;
useAuthInstance: UseAuth;
}) {
return (
<View style={styles.container}>
<View
style={{ <=======inline style works
flex: 3,
backgroundColor: 'maroon',
alignItems: 'center',
justifyContent: 'center',
}}
>
<SvgXml
xml={enterPINSVG}
style={{
display: 'flex',
backgroundColor: 'pink',
}}
/>
</View>
<View style={styles.welcomeTextWrapper}>
<Text style={styles.welcomeText}>Welcome to</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flexDirection: 'column',
flex: 1,
backgroundColor: '#DD1521',
},
logoWrapper: {},
welcomeTextWrapper: {
flex: 1,
backgroundColor: '#DD1521',
alignItems: 'center',
display: 'flex',
},
welcomeText: {
color: '#fff',
fontSize: 22,
fontWeight: '600',
},
});
Could someone explain why inline styles are working and styles via styles.create aren't?
To work with svg, add react-native-svg-transformer in your project,
Example Code :
import Logo from "./logo.svg";
<Logo width={120} height={40} />
Don't forget to configure the module with respect to expo or react native cli.
Checkout the docs for more details.

How to implement inner corner radius in triangle in react-native

I have tried all possible ways to implement an inner border radius but I can't implement this. Have you anyone know about this.
I have referred this but I can't draw this layout.
Hey you can achive this using 2 methods
stacking the views
using svg
working example: https://snack.expo.io/#ashwith00/honest-peach
code:
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import { Svg, Path } from 'react-native-svg';
const RADVALUE = 40;
const SIZE = 200;
export default function App() {
return (
<View style={styles.container}>
<View style={styles.traingleCorner}>
<View style={styles.coloredPart} />
<View style={styles.uncoloredPart} />
</View>
<View style={styles.traingleCorner}>
<Svg
width={`${SIZE}`}
height={`${SIZE}`}
fill="red"
strokeWidth={1}
viewBox={`0 0 ${SIZE} ${SIZE}`}>
<Path
d={`M0 0L0 ${SIZE}L${SIZE} ${SIZE}Q${RADVALUE} ${
SIZE - RADVALUE
} 0 0 `}
/>
</Svg>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
traingleCorner: {
width: SIZE,
height: SIZE,
},
coloredPart: {
flex: 1,
backgroundColor: 'red',
},
uncoloredPart: {
...StyleSheet.absoluteFillObject,
borderBottomLeftRadius: SIZE,
backgroundColor: '#ecf0f1',
},
});
In svg just change SIZE, RADVALUE as your requirements

How to properly set the backgroundColor for a screen imported in React-native?

i still learning react-native and want to set the backgroundColor to an entire screen which is imported in app.js.
Even if i set the flex in 1, the height covering the screen but the width is always center but not covering the entire screen.
Here what i'm doing:
App.js
import { StatusBar } from "expo-status-bar";
import React from "react";
import { StyleSheet, Text, View } from "react-native";
import Splash from "./Screen/Splash";
import TestScreen from "./Screen/TestScreen";
export default function App() {
return (
<View style={styles.container}>
{/* <Splash /> */}
<TestScreen />
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
// flexDirection: "column",
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});
TestScreen.js:
import React from "react";
import { View, Text, StyleSheet } from "react-native";
const Test = () => {
return (
<View style={styles.container}>
<Text>Hello</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
backgroundColor: "rgb(0, 168, 150)",
},
});
export default Test;
So,how i can set properly the backgroundColor to ScreenTest to cover the entire screen?
Thanks
Within app.js maybe add the full-width property:
<TestScreen style = {styles.redbox} >
container: {
flex: 1
},
redbox: {
width: 100,
height: 100,
backgroundColor: 'red'
},

How to add blur effect in react native?

How to add blur to a View in React Native, just like we apply it to an Image or BackgroundImage? If the view has translucent background using RGBA, I want to add blur to it also.
Sample Code
<ImageBackground style={styles.bg}>
<View style={styles.content} />
</ImageBackground>
const styles = StyleSheet.create({
bg: {
width: "100%",
height: "100%"
},
content:{
width: "70%",
height: "70%",
backgroundColor: "rgba(255,255,355,0.4)",
alignSelf: "center"
}
})
The easiest way is to do something like:
import React, { Component } from 'react';
import { View, Text, ImageBackground, StyleSheet } from 'react-native';
const BlurImage = () => {
return (
<ImageBackground source={require('your picture')} style={styles.ImageBackground}>
<View style={styles.container}>
<Text style={styles.text}> CSS Tricks </Text>
<Text style={styles.text}>You can style your Text and View as you deem fit</Text>
</View>
</ImageBackground>
);
}
export default BlurImage;
const styles = StyleSheet.create({
ImageBackground: {
flex: 1,
width: null,
height: null,
},
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'rgba( 0, 0, 0, 0.6 )',
},
text: {
color: 'white',
fontSize: 24
}
});
You can use opacity
const styles = StyleSheet.create({
bg: {
width: “100%”,
height: “100%”,
Opacity:0.5
},
content:{
width: “70%”,
height: “70%”,
backgroundColor: “rgba(255,255,355,0.4)”,
alignSelf: “center”
}
})