Parallel view inside a view in ReactNative - react-native

I'm trying to generate a component with a search bar inside a view and a couple of buttons inside other view. Something like this:
Expected
I develop this piece of code but I'm not able to do this parallel view.
import React from "react";
import { StyleSheet, View, FlatList, Image, Text, Item } from "react-native";
import { colorUtil } from "../../constants/Colours";
import { SearchBar, Button } from 'react-native-elements';
export default class App extends React.Component {
state = {
search: '',
};
updateSearch = search => {
this.setState({ search });
};
render() {
const { search } = this.state;
const styles = StyleSheet.create({
searchBarContainer: {
borderBottomWidth: 1,
borderBottomColor: '#e2e2e2',
height: 64,
backgroundColor: '#FFFFFF'
},
searchBarField: {
position: 'relative',
margin: 0,
width: '48%',
//padding: 44,
//fontSize: 14,
borderRadius: 80,
backgroundColor: '#E5E7E8'
},
btnField: {
borderBottomWidth: 1,
borderBottomColor: '#e2e2e2',
height: 64,
backgroundColor: '#FFFFFF'
}
});
return (
<View style={styles.searchBarContainer}>
<View style={styles.searchBarField}>
<SearchBar
lightTheme
onChangeText={this.updateSearch}
onClearText={this.updateSearch}
value={search}
icon={{ type: 'font-awesome', name: 'search' }}
placeholder='Find' />
</View>
<View style={styles.btnField}>
<Button
title="Solid Button"
/>
</View>
</View>
);
}
}
But the result is not equal to. How can I make this fields parallel?

You have to set the flex direction of parent view to row to place everything in the same line.
Also removing the height property will align the content properly.
searchBarContainer: {
borderBottomWidth: 1,
borderBottomColor: '#e2e2e2',
height: 64,
backgroundColor: '#FFFFFF',
flexDirection: 'row',
alignItems: 'center',
},
btnField: {
borderBottomWidth: 1,
borderBottomColor: '#e2e2e2',
backgroundColor: '#FFFFFF',
},

Related

Navigating among three screens

I am about to making an app with React Native and I have three screens with deferent styles (themes). I want to navigate among these three screens, So I am passing data from my main screen (App.js) as parent to the other three as child (screen1, screen2, screen3). I used a modal which has three button in it and i want whenever I pressed one of them, the screen change to the pressed one. This is my App.js file
import React, { useState, useEffect } from "react";
import { StyleSheet, View } from "react-native";
import Screen1 from "./screens/CalcScreen";
import Screen2 from "./screens/Screen1";
import Screen3 from "./screens/Screen2";
export default function App() {
const [whiteScreen, setWhiteScreen] = useState(false);
const [darkScreen, setDarkScreen] = useState(false);
const [pinkScreen, setPinkScreen] = useState(false);
const whiteScreenHandler = () => {
setWhiteScreen(true);
setDarkScreen(false);
setPinkScreen(false);
};
const darkScreenHandler = () => {
setDarkScreen(true);
setWhiteScreen(false);
setPinkScreen(false);
};
const pinkScreenHander = () => {
setPinkScreen(true);
setWhiteScreen(false);
setDarkScreen(false);
};
let content = <screen1 setDarkScreen={darkScreenHandler} />;
let content2 = <Screen2 setWhiteScreen={whiteScreenHandler} />;
let content3 = <Screen3 setPinkScreen={pinkScreenHander} />;
if (darkScreen) {
content = content;
} else if (whiteScreen) {
content = content2;
} else if (pinkScreen) {
content = content3;
}
return <View style={styles.container}>{content}</View>;
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#374351",
},
});
And this is one of my screens and my modals in my screens, the other two screens are same in coding but not in styles and I used this modal in each three screens,(does it right to use in three screens?) anyway whenever I'm pressing modal's button to change the screens i got props.screen handler() is not a function. What is wrong in my code?
import React, { useState } from "react";
import {
StyleSheet,
Text,
ScrollView,
View,
TouchableOpacity,
Alert,
Animated,
Modal,
Pressable,
} from "react-native";
const Screen1 = (props) => {
return (
<View style={styles.screen}>
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={() => {
setModalVisible(!modalVisible);
}}
>
<View style={styles.centeredView}>
<View style={styles.modalView}>
<View style={styles.button}></View>
<Text style={styles.modalText}>Themes</Text>
<TouchableOpacity
style={styles.darkTheme}
onPress={() => {
props.setDarkScreen();
}}
>
<Text>Dark</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.whiteTheme}
onPress={() => {
props.setWhiteScreen();
}}
>
<Text>White</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.pinkTheme}
onPress={() => {
props.setPinkScreen();
}}
>
<Text>Pink</Text>
</TouchableOpacity>
<Pressable
style={[styles.buttonClose]}
onPress={() => setModalVisible(!modalVisible)}
>
<Text style={styles.textStyle}>X</Text>
</Pressable>
</View>
</View>
</Modal>
<Pressable
style={[styles.button, styles.buttonOpen]}
onPress={() => setModalVisible(true)}
>
<Text style={styles.openText}>{">"}</Text>
</Pressable>
</View>
)
}
const styles = StyleSheet.create({
screen: {
backgroundColor: "#fafcff",
},
centeredView: {
// flex: 1,
marginStart: 15,
justifyContent: "center",
alignItems: "center",
// marginTop: 70,
},
modalView: {
marginTop: 200,
backgroundColor: "rgba(255,255,255,0.5)",
borderRadius: 20,
padding: 20,
alignItems: "center",
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.5,
shadowRadius: 4,
elevation: 5,
},
button: {
borderRadius: 15,
padding: 10,
elevation: 2,
justifyContent: "center",
alignItems: "center",
marginTop: 20,
},
buttonOpen: {
backgroundColor: "rgba(1,143,132,0.6)",
position: "absolute",
marginTop: 45,
marginStart: -25,
elevation: 2,
height: 70,
width: 40,
},
buttonClose: {
backgroundColor: "rgba(1,1,1,1)",
height: 40,
width: 40,
borderRadius: 50,
// padding: 10,
elevation: 2,
justifyContent: "center",
alignItems: "center",
marginTop: 20,
},
textStyle: {
color: "white",
// fontWeight: "bold",
textAlign: "center",
},
modalText: {
marginBottom: 10,
textAlign: "center",
},
openText: {
fontSize: 25,
fontWeight: "bold",
color: "white",
textAlign: "right",
},
});
export default Screen1;
You can easily doing that by using "React Native Navigation". Here is the documentation:
https://reactnavigation.org/docs/getting-started. I also recommend you that you might want to create your screens into different files to make your code cleaner.

Cannot display the second product after i clicked first one in React native app

I'm trying to develop an app with React Native.
When I clicked on any product, I can see the product detail page correctly. But for the second click, the same product always appears as keep in a cache or something.
What am I missing out?
Maybe it is a state problem but I cannot figure it out.
Product Listing Page:
import { NavigationHelpersContext } from "#react-navigation/core";
import React, { Component } from "react";
import {
SafeAreaView,
ScrollView,
StatusBar,
View,
StyleSheet,
FlatList,
Text,
Image,
TouchableOpacity,
Alert,
useColorScheme,
} from "react-native";
import { getData } from "./loadData";
export default class Pekmez extends Component {
constructor(props) {
super(props);
this.state = {
refreshFlag: (Boolean = false),
data: [],
};
}
componentDidMount() {
this._unsubscribe = this.props.navigation.addListener("focus", () => {
const { route, navigation } = this.props;
const { params } = route;
console.log("_KAT fetch params:", params);
let suffix = "Product/" + params.id;
console.log("_KAT product:", suffix);
getData(suffix, "GET").then((val) => {
this.setState({ data: val.data }, () => {
console.log("state=>", this.state);
});
});
});
//id gelecek
//fetch
}
clickEventListener(item) {
this.props.navigation.navigate("UrunDetay", { ...item });
}
render() {
return (
<View style={styles.container}>
<View style={styles.headerbar}>
<Text style={styles.pagetitle}>Ürünler</Text>
</View>
<FlatList
style={styles.list}
contentContainerStyle={styles.listContainer}
data={this.state.data}
horizontal={false}
numColumns={2}
keyExtractor={(item) => {
return item.id;
}}
renderItem={({ item }) => {
return (
<TouchableOpacity
style={styles.card}
onPress={() => {
this.clickEventListener(item);
}}
>
<View style={styles.cardFooter}></View>
<Image
style={styles.cardImage}
source={{
uri:
"https://www.toptankoyurunleri.com/Uploads/Products/" +
item.id +
"/" +
item.filename,
}}
/>
<View style={styles.cardHeader}>
<View
style={{ alignItems: "center", justifyContent: "center" }}
>
<Text style={styles.title}>{item.title}</Text>
</View>
</View>
</TouchableOpacity>
);
}}
/>
</View>
);
}
}
const styles = StyleSheet.create({
headerbar: {
height: 55,
backgroundColor: "#f27a1a",
alignItems: "center",
justifyContent: "center",
paddingVertical: 15,
marginBottom: 10,
},
pagetitle: {
fontSize: 18,
flex: 1,
alignSelf: "center",
color: "#fff",
fontWeight: "bold",
},
container: {
flex: 1,
marginTop: 0,
},
list: {
paddingHorizontal: 5,
},
listContainer: {
alignItems: "center",
},
/******** card **************/
card: {
shadowColor: "#00000021",
shadowOffset: {
width: 0,
height: 6,
},
shadowOpacity: 0.37,
shadowRadius: 7.49,
elevation: 12,
marginVertical: 10,
backgroundColor: "white",
flexBasis: "42%",
marginHorizontal: 10,
},
cardHeader: {
paddingVertical: 10,
paddingHorizontal: 5,
borderTopLeftRadius: 1,
borderTopRightRadius: 1,
flexDirection: "row",
alignItems: "center",
justifyContent: "center",
},
cardContent: {
paddingVertical: 5,
paddingHorizontal: 6,
},
cardFooter: {
flexDirection: "row",
justifyContent: "space-between",
paddingTop: 5,
paddingBottom: 5,
paddingHorizontal: 10,
borderBottomLeftRadius: 1,
borderBottomRightRadius: 1,
},
cardImage: {
height: 150,
width: 150,
alignSelf: "center",
},
title: {
fontSize: 13,
flex: 1,
alignSelf: "center",
color: "#696969",
fontWeight: "bold",
},
});
Product Detail Page
import React, { Component } from "react";
import {
SafeAreaView,
TouchableOpacity,
ScrollView,
StatusBar,
View,
StyleSheet,
Text,
Image,
useColorScheme,
Button,
BackHandler,
} from "react-native";
import {
Colors,
DebugInstructions,
Header,
LearnMoreLinks,
ReloadInstructions,
} from "react-native/Libraries/NewAppScreen";
import { WebView } from "react-native-webview";
export default class UrunDetay extends Component {
constructor(props) {
super(props);
this.state = {
detailData: {},
};
}
componentDidMount() {
console.log("this props detail:", JSON.stringify(this.props));
const { route, navigation } = this.props;
const { params } = route;
console.log("params:", params);
this.setState({ detailData: params });
}
render() {
const { detailData } = this.state;
console.log("deail:", this.props.navigation);
let vw = <Text>Loading...</Text>;
if (detailData.id && detailData.id > 0) {
vw = (
<View style={styles.container}>
<View style={styles.headerbar}>
<Text style={styles.pagetitle}>{this.state.detailData.title}</Text>
</View>
<WebView
source={{
uri:
"https://app.toptankoyurunleri.com/urun/urunadi/" +
this.state.detailData.id,
forceReload: this.state.forceReload,
}}
style={{ marginTop: 55 }}
/>
</View>
);
}
return vw;
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
headerbar: {
height: 55,
backgroundColor: "#fff",
flex: 1,
position: "absolute",
top: 0,
width: "100%",
paddingBottom: 15,
paddingTop: 15,
},
pagetitle: {
fontSize: 18,
flex: 1,
alignSelf: "center",
color: "#000",
fontWeight: "bold",
},
});
Where did you map your item in your list ? It appear to keep the same item or item.id in your state can you make test with breakpoints on your browser tool ?

Why is my style not applied to my child component?

Good evening,
I have been trying to create custom card Component in react native that i can use everywhere i need it.
I apply a part of the style inside the component itself, and for the width, layout etc, where i need it. Then i am trying to use the spread operator to "merge" all the properties in the style of the child component. I saw it in a video tutorial, but it's not working for me, and i don't see what is wrong in my code :
import React from 'react';
import { View, Text, StyleSheet, TextInput, Button } from 'react-native';
import Card from '../components/Card';
import Color from '../constants/colors'
import Input from '../components/Input'
const StartGameScreen = props => {
return (
<View style={styles.screen}>
<Text style={styles.title}>Start a New Game!</Text>
<Card style={styles.inputContainer}>
<Text>Select a Number</Text>
<Input />
<View style={styles.buttonContainer}>
<Button title="Reset" color={Color.accent} onPress={() => {}} />
<Button title="Confirm" color={Color.primary} onPress={() => {}} />
</View>
</Card>
</View>
);
};
const styles = StyleSheet.create({
screen: {
flex: 1,
padding: 10,
alignItems: 'center'
},
title: {
fontSize: 20,
marginVertical: 10
},
inputContainer: {
width: 300,
maxWidth: '80%',
alignItems: 'center',
},
buttonContainer: {
flexDirection: 'row',
width: '100%',
justifyContent: 'space-between',
paddingHorizontal: 15
},
input: {
width: 50
}
});
export default StartGameScreen;
And the child component :
import React from 'react';
import { View, StyleSheet } from 'react-native';
const Card = props => {
return (
<View style={{ ...styles.card, ...props.style }}> {props.children}</View>
);
};
const styles = StyleSheet.create({
card: {
shadowColor: 'black',
shadowOffset: { width: 0, height: 2 },
shadowRadius: 6,
shadowOpacity: 0.26,
elevation: 8,
backgroundColor: 'white',
padding: 20,
borderRadius: 10
}
});
export default Card;
Note : I tried with input and card, it's working for none of them.
Is that something that we were able to do but we can't anymore ?
Thank you
Have a nice weekend
Try this:
const Card = props => {
return (
<View style={[styles.card, props.style ]}> {props.children}</View>
);
};

React Native dropDown Menu can not be clicked

I have a dropDown Menu which is needed to be clicked, but when I click on its items, the underlying items which are a searchBar and a Flatlist which is also clickable, are clicked. I want the menu to be clicked, not the searchBar or Flatlist. what should i do?
Here is a screenshot of my simulator:
I can not click on "Arabic" and "persian" on the menu.
Here is parts of my code:
import React, { Component } from 'react';
import {StyleSheet, Text, View, FlatList, TouchableOpacity } from 'react-native';
import { List, ListItem, SearchBar } from 'react-native-elements';
import DropdownMenu from 'react-native-dropdown-menu';
import {Header, Left, Right, Icon} from 'native-base'
var SQLite = require('react-native-sqlite-storage')
var db = SQLite.openDatabase({name: 'test.sqlite', createFromLocation: '~dictionary.sqlite'})
var data = [["English", "Arabic", "Persian"]];
export default class App extends Component {
constructor(props) {
super(props)
...
db.transaction((tx) => {
...
}
searchFilterFunction = text => {
...
};
render() {
return (
<View style = {styles.container}>
<Header style={styles.headerStyle}>
...
</Header>
<View style={{height: 3 }}/>
<View style={styles.menuView}>
<DropdownMenu
bgColor={"#B38687"}
activityTintColor={'green'}
titleStyle={{color: '#333333'}}
zIndex={100}
handler={(selection, row) => this.setState({text4: data[selection][row]})}
data={data}
>
</DropdownMenu>
<Icon name="arrow-round-forward" style={styles.iconStyle}/>
<DropdownMenu
bgColor={"#B38687"}
activityTintColor={'green'}
titleStyle={{color: '#333333'}}
zIndex={100}
handler={(selection, row) => this.setState({text: data[selection][row]})}
data={data}
>
</DropdownMenu>
</View >
<View >
<View style={styles.viewBorder}>
<SearchBar
...
/>
</View>
<View style={styles.viewBorder}>
<List containerStyle={{ flexDirection: 'column-reverse', borderTopWidth: 0, borderBottomWidth: 0 }} >
<FlatList
...
onPress={() =>{this.props.navigation.navigate('Meaning', {data: (item.word_english +'\n' + item.word_arabic)} ); }}
...
/> )}
/>
</List>
</View>
</View>
</View>);}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#CBEFDD',
// flexDirection: 'column'
},
iconStyle:{
backgroundColor:'#CBEFDD',
color:'#84B071',
marginLeft: '5%',
marginRight: '5%',
marginTop: 7,
// height:30,
// alignContent:'center'
},
menuView:{
// flex:1,
// height: 50,
marginLeft: 3,
marginRight: 3,
flexDirection:'row',
zIndex:100,
},
rezvanText:{
flex:1 ,
fontSize: 20,
color: 'white',
justifyContent:'flex-end'
},
headerStyle:{
backgroundColor : "#84B071"
},
viewBorder: {
backgroundColor: 'white',
borderWidth: 2,
borderColor: '#B38687',
marginVertical:3,
marginLeft: 3,
marginRight: 3
}
});
Thanks to Andrew and this I could solve the problem, I put the answer incase someone needs it!
The solution is to assign a minHeight to the View which contains the DropDown menu, and also assign a position: 'absolute' to it. Then set the constraints in order to keep them from overlapping, and Voila!.. I repost the styles here:
const styles = StyleSheet.create({
container: {
...
},
iconStyle:{
...
},
menuView:{
// flex:1,
// height: 50,
**minHeight:215,**
marginLeft: 3,
marginRight: 3,
marginTop:60,
flexDirection:'row',
zIndex:100,
**position: 'absolute'**
},
rezvanText:{
...
},
headerStyle:{
...
},
searchBarView: {
backgroundColor: 'white',
borderWidth: 2,
borderColor: '#B38687',
**marginTop:51,**
marginLeft: 3,
marginRight: 3
},
flatListVew:{
backgroundColor: 'white',
borderWidth: 2,
borderColor: '#B38687',
marginVertical:3,
marginLeft: 3,
marginRight: 3
}
});

Creating an object in react native

I am trying to save the noteText inside a text, but it's becoming a problem. I would like to make an object with the same attributes: name, date, note and to display only the name and date. Also, I would like, when I create a Noteto map it in an array. Like so
let notes = this.state.noteArray.map((val, key)=>{
return <Note key={key} keyval={key} val={val}
deleteMethod={()=>this.deleteNote(key)} openNote={()=>this.openNote(key)}/>
});
And here is how I add an item:
this.props.navigation.state.params.noteArray.push({
'noteNumber':'Note '+ this.props.navigation.state.params.noteArray[key],
'date':d.getFullYear()+
"/"+(d.getMonth()+1) +
"/"+ d.getDate(),
'note': this.state.noteText
});
Here is the class note:
import React, { Component } from 'react';
import {
View,
Text,
StyleSheet,
TouchableOpacity,
TouchableWithoutFeedback,
} from 'react-native';
export default class Note extends Component {
render() {
return (
<TouchableWithoutFeedback key={this.props.keyval} onPress={this.props.openNote}>
<View style={styles.note}>
<Text style={styles.noteDate}>{this.props.val.noteNumber}</Text>
<Text style={styles.noteDate}>{this.props.val.date}</Text>
<Text style={styles.noteText}>{this.props.val.note}</Text>
<TouchableOpacity onPress={this.props.deleteMethod} style={styles.noteDelete}>
<Text style={styles.noteDeleteText}>Del</Text>
</TouchableOpacity>
</View>
</TouchableWithoutFeedback>
);
}
}
const styles = StyleSheet.create({
note: {
position: 'relative',
padding: 20,
backgroundColor:'#fff',
paddingRight: 100,
borderLeftWidth:1,
borderLeftColor: '#000',
borderRightWidth:1,
borderRightColor: '#000',
borderBottomWidth:1,
borderBottomColor: '#000'
},
noteDate:{
paddingLeft: 20,
borderLeftWidth: 10,
borderLeftColor: '#0000FF'
},
noteText: {
paddingLeft: 20,
borderLeftWidth: 10,
borderLeftColor: '#0000FF',
opacity: 0,
},
noteDelete: {
position: 'absolute',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#2980b9',
padding: 10,
top: 10,
bottom: 10,
right: 10
},
noteDeleteText: {
color: 'white'
},
});
Here is the example I worked on:
import React, { Component } from "react";
class Counter extends Component {
state = { counter: this.props.startsWith };
...
}
In stead of Counter, I used Note itself.