Gradient slider not working in react-native - react-native

I am trying to make a gradient slider for my app in react-native, but I keep getting the message' requireNativeComponent : "BVLinearGradient" was not found in the UIManager' when I run my code.
I tried adding different libraries and other methos that poeple used to fix the same problem. But nothing works.
import React, { useState, useEffect} from "react";
import {AppRegistry, StyleSheet, Text, View, TouchableOpacity,
SafeAreaView, Image, useWindowDimensions, onPress, TextInput,
Animated} from 'react-native';
import { useNavigation, getStateFromPath } from '#react-
navigation/native';
import Slider from '#react-native-community/slider'
import { firebase } from '../config'
import Logo from '../assets/temporaryLogoApp.png'
import MultiSlider from '#ptomasroos/react-native-multi-slider'
import LinearGradient from 'react-native-linear-gradient';
const SliderScreen = () => {
const [value, setValue] = useState(0);
return(
<View>
<Text style={styles.hoi}>
hoi
</Text>
<View
style={{
width: 100,
height: 100,
backgroundColor: value === 2 ? 'green' : 'grey',
}}
/>
</View> */}
<View style={styles.container}>
<LinearGradient
colors={['#ff0000', '#00ff00', '#0000ff']}
start={{ x: 0, y: 0 }}
end={{ x: 1, y: 0 }}
style={styles.gradient}
>
<Slider
minimumValue={0}
maximumValue={1}
value={value}
onValueChange={setValue}
style={styles.slider}
thumbTintColor="#fff"
minimumTrackTintColor="#transparent"
maximumTrackTintColor="#transparent"
/>
</LinearGradient>
<Text>Value: {value}</Text>
</View>
</View>
)
}
export default SliderScreen
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
gradient: {
height: 40,
width: '100%',
borderRadius: 20,
overflow: 'hidden',
},
slider: {
transform: [
{
translateX: -20,
},
],
},
});

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>
); }

How to design React-Native Button

I am new to react-native and I want to design such a button, I tried and searched but could not find an answer
any help would be appreciated
Look at this aproach, with radiusBorder you can achieve something close to you're looking for:
import React from 'react';
import { View, StyleSheet, TouchableOpacity, Text } from 'react-native';
export default function NAME() {
return (
<TouchableOpacity>
<View style={styles.wrapperContainer}>
<View style={styles.Container}>
<Text>Foo</Text>
</View>
</View>
</TouchableOpacity>
);
}
const styles = StyleSheet.create({
Container: {
height: 50,
width: 100,
alignItems: 'center',
background: '#1f90f0',
borderRadius: 5,
justifyContent: 'center',
borderTopLeftRadius: 50,
borderLeftWidth: 2,
borderLeftColor: 'lightgray'
},
wrapperContainer: {
background: 'lightgray',
width: 100,
}
});
You can create a custom button in react-native by using TouchableOpacity and if you want to perform any function when the user press that button you can use onPress prop.
import React from 'react';
import {TouchableOpacity, View, Text} from 'react-native';
const FunctionName = () => {
return(
<View>
<TouchableOpacity style={styles.buttonStyling}>
<Text>Button Name</Text>
</TouchableOpacity>
</View>
);
}
export defaultFunctionName;
Use TouchableOpacity instead. That way you can customize the button however you want it to be.
https://reactnative.dev/docs/touchableopacity
import React from 'react';
import {TouchableOpacity, View, Text} from 'react-native';
export default function NAME() {
return(
<View>
<TouchableOpacity style={{padding: 10, borderWidth: 1, borderColor: '#000'}}>
<Text>Text goes here</Text>
</TouchableOpacity>
</View>
);
}
Edit: This is the closest I got for what you are looking for.
import React from 'react';
import { View, StyleSheet, TouchableOpacity } from 'react-native';
export default function NAME() {
return (
<TouchableOpacity>
<View style={styles.Container}>
<View style={styles.ButtonShape} />
</View>
</TouchableOpacity>
);
}
const styles = StyleSheet.create({
Container: {
backgroundColor: 'transparent',
},
ButtonShape: {
borderLeftWidth: 20,
width: 120,
borderLeftColor: 'transparent',
borderRightColor: 'transparent',
borderTopColor: 'blue',
borderTopWidth: 50,
},
});

How can I find the distance between two elements React Native?

import React, { useState, useEffect, useRef } from 'react';
import { StyleSheet, View, Text, Animated, UIManager, findNodeHandle, PanResponder } from 'react-native';
const TestScreen = props => {
const myPosition = useRef();
const enemyPosition = useRef();
return (
<View style={styles.main}>
<View style={styles.screenLeft}>
<Animated.View ref={myPosition}>
<View style={styles.fighter} >
</View>
</Animated.View>
</View>
<View style={styles.screenRight}>
<Animated.View ref={enemyPosition}>
<View style={styles.enemy}>
</View>
</Animated.View>
</View>
</View>
);
}
const styles = StyleSheet.create({
main: {
flex: 1,
},
screenLeft: {
alignItems: 'flex-start',
position: 'absolute'
},
screenRight: {
alignItems: 'flex-end'
},
fighter: {
backgroundColor: '#000',
width: 20,
height: 50
},
enemy: {
backgroundColor: '#ff0000',
width: 20,
height: 50
}
});
export default TestScreen;
enter image description here
Please use Dimensions
import { Dimensions } from 'react-native';
Then get the windowWidth
const windowWidth = Dimensions.get('window').width;
According to your case, it should be windowWidth - 40
= windowWidth -20 (the left hand side box) -20 (the right hand side box)

React Native: React Navigation using reusable component

I'am new in React Native & I'am trying to create separated Button component file (reusable component), and when I press the button it will navigate to another page (react-navigation). Below is my code:
GlobalButton.js
import React from 'react'
import { StyleSheet, TouchableOpacity, Text} from 'react-native'
import { useNavigation, NavigationContainer } from '#react-navigation/native';
const GlobalButton = (props, {screenName}) => {
const navigation = useNavigation();
return
<TouchableOpacity style={[styles.btn, props.style]} onPress= {() => navigation.navigate(screenName)}>
{props.children}
</TouchableOpacity>
}
const styles = StyleSheet.create({
btn: {
alignItems: 'center',
textAlign: 'center',
borderRadius: 25,
height: 50,
}
})
export default GlobalButton;
LoginPage.js
import React from 'react'
import { StyleSheet, Text, View, Image, TextInput, Button} from 'react-native'
import ButtonLogin from '../components/GlobalButton'
// import { useNavigation, NavigationContainer } from '#react-navigation/native';
export default function LoginPage() {
return (
<View style={styles.penampung}>
<Image source= {require('../assets/beetle.png')} style={styles.gambar}/>
<TextInput style={styles.textField} placeholder='username' />
<TextInput style={styles.textField} placeholder='password' secureTextEntry={true} />
<ButtonLogin style={styles.btnStyle} screenName="MainPage"><Text style={styles.text}>LOGIN</Text></ButtonLogin>
</View>
)
}
const styles = StyleSheet.create({
penampung: {
flex: 1,
alignItems: 'center',
backgroundColor: '#00688B',
justifyContent: 'center'
},
gambar: {
height: 100,
width: 100,
},
textField: {
marginTop: 25,
backgroundColor: '#cafafe',
borderRadius: 25,
width: '80%',
height: 50,
paddingLeft: 20
},
btnStyle: {
padding: 10,
position: 'absolute',
bottom: 20,
backgroundColor: "#fc4445",
width: '80%'
},
text:{
fontSize: 20,
fontWeight: 'bold',
color: 'white'
}
})
What I'am expecting is that when I click the login button it should navigate to the MainPage.js, but instead it gives error message ->
Error: You need to specify name or key when calling navigate with an object as the argument.
Please anyone help me in this matter, Thanks...
You should do like below as screenName is part of your props,
<TouchableOpacity style={[styles.btn, props.style]} onPress= {() => navigation.navigate(props.screenName)}>

How do you use flex inside a scrollView React Native

When I use the ScrollView with contents that have flex they don't show and flex doesn't seem to work. How do you get flex to work inside a ScrollView?
import React, { Component } from 'react';
import {
Text,
View,
StyleSheet,
ScrollView,
TouchableOpacity,
Dimensions,
} from 'react-native';
const App = (props) => {
return (
<ScrollView style={styles.body}>
<View style={styles.box1} />
<View style={styles.box2} />
<View style={styles.box1} />
</ScrollView>
);
};
const styles = StyleSheet.create({
body: {
flex: 1,
flexDirection: 'column',
backgroundColor: '#fff',
borderColor: 'red',
borderWidth: 5
},
box1: {
flex: 1,
backgroundColor: 'blue',
},
box2: {
flex: 2,
backgroundColor: 'purple',
}
});
export default App;
I've created a snack. The inner views/boxes don't show.
I managed to figure it out. You create another view inside the scrollView and fix the height. The only problem is that the size of the content views depends on the height of this view instead of the height of the scrollView.
import React, { Component } from 'react';
import {
Text,
View,
StyleSheet,
ScrollView,
TouchableOpacity,
Dimensions,
} from 'react-native';
// import { useHeaderHeight } from '#react-navigation/stack';
const App = (props) => {
let screenHeight = Dimensions.get('window').height;
// let headerHeight = useHeaderHeight();
let headerHeight = 0
let bodyHeight = screenHeight - headerHeight + 500;
return (
<ScrollView style={styles.body}>
<View style={[styles.innerBody, { height: bodyHeight }]}>
<View style={styles.box1} />
<View style={styles.box2} />
<View style={styles.box1} />
</View>
</ScrollView>
);
};
const styles = StyleSheet.create({
body: {
flex: 1,
flexDirection: 'column',
backgroundColor: '#fff',
},
innerBody: {
borderColor: 'red',
borderWidth: 5
},
box1: {
flex: 1,
backgroundColor: 'blue',
},
box2: {
flex: 2,
backgroundColor: 'purple',
}
});
export default App;
Heres a snack showing it working.