React Native - Button Styling Issues - reactjs

I'm confused as to why my button doesn't seem to resemble the default native react button with a square shape that spans to the max width of the viewport - I've researched and understand from the react docs that the native button is very limited in terms of styling and props, but mine seems to be missing the default styling - any tips? My button component can be found here
I've tried adding backgroundColor to the styles, color, etc. but nothing changes

try this... customise further as you required
you will love react-native and to style after this :)
import React, { useState } from "react";
import { StyleSheet, View, TextInput, TouchableOpacity } from "react-native";
export default function AddTodo({ submitHandler }) {
const [text, setText] = useState("");
const changeHandler = (val) => {
setText(val);
};
return (
<View>
<TextInput
style={styles.input}
placeholder="add a game to the docket..."
placeholderTextColor="white"
onChangeText={changeHandler}
clearTextOnFocus="true"
/>
<TouchableOpacity style={styles.btn} onPress={() => submitHandler(text)}>
<Text style={styles.btnText}>add game</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
input: {
marginBottom: 10,
paddingHorizontal: 8,
paddingVertical: 6,
backgroundColor: "#2DCCCF",
flexDirection: "row",
color: "white",
},
btn: {
height: 45,
borderRadius: 8,
elevation: 3,
backgroundColor: "#AB47BC",
justifyContent: "center",
},
btnText: {
color: "#fff",
},
});

Related

react native typescript get event target

i am new to react native with typescript and i have a problem
i want to get the text from the Text component so it mean when i press the Text i will console log it
i cant get the target value
i try also currentTarget
but it will give me a huge array of object with ton of keys without any target value
import { GestureResponderEvent, StyleSheet, Text, View } from "react-native";
import React, { useState } from "react";
import Card from "../../components/card/Card";
import ChooseLotteryNumber from "../../components/choose_lottery_number/ChooseLotteryNumber";
const CreateUser = () => {
const [selectedNumbers, setSelectedNumbers] = useState<number[]>([]);
const onSelectNumbers = (e: GestureResponderEvent )=>{
console.log(e.currentTarget)
};
return (
<View>
<Card>
<View>
<Text onPress={onSelectNumbers}>choose numbers!</Text>
</View>
<View style={styles.number_container}>
</View>
</Card>
</View>
);
};
export default CreateUser;
const styles = StyleSheet.create({
number_container: {
flexDirection: "row",
flexWrap: "wrap",
justifyContent: "center",
backgroundColor: "white",
borderRadius: 10,
},
number: {
fontSize: 24,
margin: 6,
borderWidth: 1,
borderRadius: 5,
textAlign: "center",
width: 35,
},
});

react native background blur without npm packages

i am using animated ScrollView and Modal. and i need this blurry background on modal shown on right pic.
how can i add this background to this modal? i want blurry effect without npm packages.
<Modal transparent={true}>
you want this: https://reactnative.dev/docs/imagebackground
and use the component prop blurRadius like:
import React from "react";
import { ImageBackground, StyleSheet, Text, View } from "react-native";
const image = { uri: "https://reactjs.org/logo-og.png" };
const App = () => (
<View style={styles.container}>
<ImageBackground source={image} blurRadius={5} resizeMode="cover" style={styles.image}>
<Text style={styles.text}>Inside</Text>
</ImageBackground>
</View>
);
const styles = StyleSheet.create({
container: {
flex: 1,
},
image: {
flex: 1,
justifyContent: "center"
},
text: {
color: "white",
fontSize: 42,
lineHeight: 84,
fontWeight: "bold",
textAlign: "center",
backgroundColor: "#000000c0"
}
});
export default App;

Error: "undefined is not an object (evaluating '_reactNative.Stylesheet.create') (Device"

I am new to react native and need help. I created two components one
is for TextInput and another for button, I imported the Button
component in textInput and it show error. can someone help me figure
out the error I made here. I am getting error undefined is not an
object(evalutaing;?_reactnative,stylesheet.create')*
and folder structure in the picture attached
this is button Component
import React from 'react';
import { TouchableOpacity, Text, Stylesheet } from 'react-native';
export const Button = ({
style = {},
textStyle = {},
size = 125,
...props
}) => {
return (
<TouchableOpacity style={\[styles(size).radius, style\]}>
<Text style={\[styles.text, textStyle\]}>{props.title}</Text>
</TouchableOpacity>
);
};
**error is likely to be here**
const styles = (size) => Stylesheet.create({
radius: {
borderRadius: size / 3,
width: size,
hieght: size,
alignItems: 'center',
borderColor: 'white',
},
text: {
color: '#fff',
fontSize: 20,
},
});
this is textInput component
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import { TextInput } from 'react-native-paper';
import { Card } from 'react-native-paper';
import {Button} from '../components/Button';
// You can import from local files
// or any pure javascript modules available in npm
export const Something = () => {
return (
<View style={styles.container}>
<View style={styles.titleContainer}>
<Text style={styles.title}> input something here</Text>
<TextInput />
<Button title="+" />
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
titleContainer: {
flex: 0.5,
padding: 20,
justifyContent: 'center',
},
title: {
fontWeight: 'bold',
color: 'white',
fontSize: 30,
},
});
Mispelled height maybe?
const styles = (size) => Stylesheet.create({
radius: {
borderRadius: size / 3,
width: size,
hieght: size, <-------------------- hieght
alignItems: 'center',
borderColor: 'white',
},
text: {
color: '#fff',
fontSize: 20,
},
});

react native text component should not be larger than parent view component

Requirement:
I want to create the link badge.
Here's my code:
import React from 'react';
import EStyleSheet from 'react-native-extended-stylesheet';
import {Text, View} from 'react-native';
import HyperLinkIcon from '../../icons/hyperlink-icon';
import RemoveBadgeItemIcon from '../../icons/remove-item-badge-icon';
import {Colors, Typography, Fonts} from '../../theme';
const ItemBadge = ({
url = 'http://www.koovs.com/itemhttp://www.koovs.com/item',
}) => {
return (
<View style={styles.badgeContainer}>
<HyperLinkIcon />
<View style={styles.urlContainerStyle}>
<Text style={styles.urlStyle} numberOfLines={1}>
{url}
</Text>
</View>
<RemoveBadgeItemIcon />
</View>
);
};
export default ItemBadge;
const styles = EStyleSheet.create({
badgeContainer: {
padding: 14,
borderRadius: 12,
backgroundColor: '#F0EFFF',
flexDirection: 'row',
alignItems: 'center',
overflow: 'hidden',
},
urlContainerStyle: {
flexGrow: 1,
paddingHorizontal: 14,
},
urlStyle: {
color: Colors.LABEL,
fontSize: Typography._14,
fontFamily: Fonts.ROMAN,
},
});
But this gives me this:
I am trying to make Text component's maxWidth equal to that of its parent View and also if the url is long then it gets ellipsis.
Responsiveness should also be taken into consideration.

How to pass style props for a specific component in react-native

I tried creating a button with specific styles for its .
I had more than 3 properties like justifyContent, alignItems, backgroundColor and height. I wanted to pass a style from another component to this, so that the backgroundColor property of the button changes.
My Code:
import React from 'react';
import { Text, TouchableOpacity } from 'react-native';
const Button = ({ buttonName, csCode }) => {
const { buttonStyle, textStyle } = styles;
return (
<TouchableOpacity style={{ buttonStyle, backgroundColor: [csCode] }}>
<Text style={textStyle}>
{buttonName}
</Text>
</TouchableOpacity>
);
};
const styles = {
textStyle: {
alignSelf: 'center',
color: '#ffffff',
fontSize: 35,
fontWeight: '600',
},
buttonStyle: {
justifyContent: 'center',
alignSelf: 'stretch',
height: '20%',
}
};
export { Button };
Here, the buttonStyle is not applied to the buttons, instead only the backgroundColor prop is only being applied. Any help ?
Thanks.
If you want to use styles from the styles object and inline styles together, please put them in an array like this:
<TouchableOpacity style={[buttonStyle, {backgroundColor: csCode }]}>
...
</TouchableOpacity>

Resources