I am trying to send an array and a key to another screen using StackNavigator, but it tells me that the program does not see the getNoteArray() function.
getNoteArray(){
return this.state.noteArray;
}
editMethod(key){
const {navigate} = this.props.navigation;
navigate('EditNote' , {noteArray: this.getNoteArray(), key});
}
Here is the EditNote screen:
import React, { Component } from 'react';
import {
View,
Text,
StyleSheet,
TextInput,
TouchableOpacity,
AsyncStorage,
} from 'react-native';
import Note from './Note.js';
export default class EditNote extends Component {
static navigationOptions = {
title: 'Edit',
};
constructor(props){
super(props);
this.state = {
noteArray: [],
noteText: '',
};
}
componentDidMount(){
this.setState({noteArray: this.props.navigation.state.params.noteArray})
alert(this.state.noteArray);
}
render() {
const {params} = this.props.navigation.state;
return (
<View style={styles.container}>
<View style={styles.noteBody}>
<TextInput
multiline = {true}
numberOfLines = {1000000}
style={styles.textInput}
placeholderTextColor='grey'
underlineColorAndroid='transparent'>
</TextInput>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
noteBody:{
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
zIndex: 10,
alignItems: 'center',
borderBottomWidth:1,
borderTopColor: '#000',
marginBottom: 100,
},
textInput: {
alignSelf: 'stretch',
textAlignVertical: 'top',
backgroundColor: '#fff',
color: '#000',
padding: 20,
borderTopWidth:2,
borderTopColor: '#ededed',
},
addButton: {
position: 'absolute',
zIndex: 11,
left: 0,
bottom: 0,
alignItems: 'center',
justifyContent: 'center',
width: 300,
backgroundColor: '#00FF00',
height: 60,
elevation: 8
},
addButtonText: {
color: '#fff',
fontSize: 24,
},
});
You can try like this
editMethod(key){
const {navigate} = this.props.navigation;
navigate('EditNote',{ params1:'hello',params2:'hi'});
}
https://reactnavigation.org/docs/en/navigation-prop.html#navigate-link-to-other-screens
The syntax is
navigation.navigate({routeName, params, action, key})
OR
navigation.navigate(routeName, params, action)
So it can be
navigate('EditNote' , { notesArray: getNoteArray(), key} );
OR
navigate({
routeName: 'EditNote' ,
params: {
notesArray: getNoteArray(),
key
}
});
Related
I am now using react-native-qrcode-scanner to read the QRcode but now have trouble when trying to add the text inside of the camera view. (Not top or bottom of the camera view but inside of it - more exactly under the marker position with align center)
The text is not displayed even though I add Text field with position: 'absolute'
import QRCodeScanner from 'react-native-qrcode-scanner';
return (
<QRCodeScanner
onRead={onSuccess}
reactivate={true}
showMarker
containerStyle={styles.cameraContainer}
cameraStyle={{
...styles.camera,
height: Dimensions.get('window').height - topOffset,
}}
topViewStyle={styles.topView}
markerStyle={styles.marker}
bottomViewStyle={styles.bottomView}
cameraProps={{
type: 'back',
}}
/>
);
const styles = StyleSheet.create({
cameraContainer: {
backgroundColor: theme.colors.background,
},
camera: {},
bottomView: {
height: 0,
flex: 0,
},
topView: {
height: 0,
flex: 0,
},
marker: {
borderWidth: 2,
borderRadius: 10,
borderColor: theme.colors.white,
}
});
You can use styling with position: absolute for the text element within a container.
Following sample works for me on iOS:
/* eslint-disable react-native/no-color-literals */
// #ts-nocheck
import React from "react"
// eslint-disable-next-line react-native/split-platform-components
import { View, StyleSheet, TouchableOpacity, Text, ToastAndroid } from "react-native"
import QRCodeScanner from "react-native-qrcode-scanner"
import { RNCamera } from "react-native-camera"
import Clipboard from "#react-native-community/clipboard"
const styles = StyleSheet.create({
cameraContainer: {
flex: 1,
},
container: {
backgroundColor: "#fff",
flex: 1,
height: "100%",
width: "100%",
},
flash: {
alignItems: "center",
backgroundColor: "#CCC",
borderRadius: 19,
height: 38,
justifyContent: "center",
position: "absolute",
right: 20,
top: 20,
width: 100,
zIndex: 1,
},
markerStyle: {
borderColor: "#ffffff",
top: -40,
},
})
export const BarcodeScannerScreen: React.FC = () => {
const [flashOn, setFlashOn] = React.useState(false)
const barcodeScanned = (barcode) => {
ToastAndroid.show("Code copied to clipboard", ToastAndroid.LONG)
Clipboard.setString(JSON.stringify(barcode.data))
console.log("Barcode: ", barcode.data)
}
const toggleFlash = () => {
setFlashOn(!flashOn)
}
const androidCameraPermissionOptions = {
title: "Camera permission required",
message: "To test QR-Code scan camera permission is required",
}
return (
<View style={styles.container}>
<TouchableOpacity style={styles.flash} onPress={toggleFlash}>
<Text>Flash on</Text>
</TouchableOpacity>
<QRCodeScanner
onRead={barcodeScanned}
cameraProps={{
androidCameraPermissionOptions: androidCameraPermissionOptions,
flashMode: flashOn
? RNCamera.Constants.FlashMode.torch
: RNCamera.Constants.FlashMode.off,
barCodeTypes: [RNCamera.Constants.BarCodeType.qr],
type: RNCamera.Constants.Type.back,
}}
showMarker={true}
reactivate={true}
reactivateTimeout={3000}
cameraStyle={styles.cameraContainer}
markerStyle={styles.markerStyle}
/>
<View
style={{
position: "absolute",
alignSelf: "center",
top: 300,
width: 50,
height: 50,
backgroundColor: "#ff00ff",
}}
>
<Text>Text</Text>
</View>
</View>
)
}
/* eslint-disable react-native/no-color-literals */
// #ts-nocheck
import React from "react"
// eslint-disable-next-line react-native/split-platform-components
import { View, StyleSheet, TouchableOpacity, Text, ToastAndroid } from "react-native"
import QRCodeScanner from "react-native-qrcode-scanner"
import { RNCamera } from "react-native-camera"
import Clipboard from "#react-native-community/clipboard"
const styles = StyleSheet.create({
cameraContainer: {
flex: 1,
},
container: {
backgroundColor: "#fff",
flex: 1,
height: "100%",
width: "100%",
},
flash: {
alignItems: "center",
backgroundColor: "#CCC",
borderRadius: 19,
height: 38,
justifyContent: "center",
position: "absolute",
right: 20,
top: 20,
width: 100,
zIndex: 1,
},
markerStyle: {
borderColor: "#ffffff",
top: -40,
},
})
export const BarcodeScannerScreen: React.FC = () => {
const [flashOn, setFlashOn] = React.useState(false)
const barcodeScanned = (barcode) => {
ToastAndroid.show("Code copied to clipboard", ToastAndroid.LONG)
Clipboard.setString(JSON.stringify(barcode.data))
console.log("Barcode: ", barcode.data)
}
const toggleFlash = () => {
setFlashOn(!flashOn)
}
const androidCameraPermissionOptions = {
title: "Camera permission required",
message: "To test QR-Code scan camera permission is required",
}
return (
<View style={styles.container}>
<TouchableOpacity style={styles.flash} onPress={toggleFlash}>
<Text>Flash on</Text>
</TouchableOpacity>
<QRCodeScanner
onRead={barcodeScanned}
cameraProps={{
androidCameraPermissionOptions: androidCameraPermissionOptions,
flashMode: flashOn
? RNCamera.Constants.FlashMode.torch
: RNCamera.Constants.FlashMode.off,
barCodeTypes: [RNCamera.Constants.BarCodeType.qr],
type: RNCamera.Constants.Type.back,
}}
showMarker={true}
reactivate={true}
reactivateTimeout={3000}
cameraStyle={styles.cameraContainer}
markerStyle={styles.markerStyle}
/>
<View
style={{
position: "absolute",
alignSelf: "center",
top: 300,
width: 50,
height: 50,
backgroundColor: "#ff00ff",
}}
>
<Text>Text</Text>
</View>
</View>
)
}
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 ?
I'm trying to show this custom modal pop-up over the top of the Header component in React Navigation. See the pic below:
I've tried setting zIndexes on the overlay and parent view containers, but the header is seperate in the component stack. Anyone know how to solve this? I've tried putting a BlurView over the header component.
import React, { Component } from "react";
import PropTypes from "prop-types";
import { StyleSheet, View, Animated, Text, TextInput } from "react-native";
import BlockButton from "../common/BlockButton";
import { BlurView } from "react-native-blur";
export default class BlurAlert extends Component {
static navigationOptions = ({ navigation }) => {
return {
headerStyle: {}
};
};
constructor(props) {
super(props);
this.state = {
visible: this.props.visible,
title: null,
body: null
};
}
render() {
if (this.props.visible) {
return (
<View style={styles.wrapper}>
<BlurView style={styles.blurView} blurType="dark" blurAmount={5} />
<View style={styles.box}>
<Text style={styles.titleText}>{this.props.title}</Text>
<Text style={styles.bodyText}>{this.props.body}</Text>
{this.props.children}
<BlockButton
backgroundColor={"#F0AC8F"}
borderColor={"#F0AC8F"}
fontColor={"white"}
title={this.props.buttonText}
padding={23}
marginTop={25}
disabled={false}
onPress={this.props.onClosed}
/>
</View>
</View>
);
} else {
return <View />;
}
}
}
BlurAlert.propTypes = {
title: String,
body: String,
buttonText: String,
visible: Boolean
};
BlurAlert.defaultProps = {
visible: false,
buttonText: "OK"
};
const styles = StyleSheet.create({
wrapper: {
position: "absolute",
top: 0,
left: 0,
bottom: 0,
right: 0,
height: "100%",
width: "100%",
zIndex: 9999
},
blurView: {
flex: 1,
zIndex: 99999
},
container: {
margin: 25,
zIndex: 99999,
position: "relative",
top: 0,
left: 0,
bottom: 0,
right: 0
},
box: {
backgroundColor: "white",
padding: 20,
position: "absolute",
top: "15%",
borderColor: "black",
borderWidth: StyleSheet.hairlineWidth,
margin: 25,
alignItems: "center",
shadowColor: "#000",
shadowOffset: { width: 0, height: 1 },
shadowOpacity: 0.1,
shadowRadius: 3,
zIndex: 99999,
width: "85%"
},
titleText: {
fontFamily: "Lora",
fontSize: 20
},
bodyText: {
marginTop: 15,
textAlign: "center",
fontFamily: "Lora",
fontSize: 16,
lineHeight: 20
}
});
You can try using "Modal":
import React, { Component } from "react";
import PropTypes from "prop-types";
import { StyleSheet, View, Animated, Text, TextInput, Modal } from "react-native";
import BlockButton from "../common/BlockButton";
import { BlurView } from "react-native-blur";
export default class BlurAlert extends Component {
static navigationOptions = ({ navigation }) => {
return {
headerStyle: {}
};
};
constructor(props) {
super(props);
this.state = {
visible: this.props.visible,
title: null,
body: null
};
}
render() {
if (this.props.visible) {
return (
<Modal
transparent
animationType="fade"
visible={this.state.visible}
onRequestClose={() => {
console.log('Modal has been closed.');
}}>
>
<View style={styles.wrapper}>
<BlurView style={styles.blurView} blurType="dark" blurAmount={5} />
<View style={styles.box}>
<Text style={styles.titleText}>{this.props.title}</Text>
<Text style={styles.bodyText}>{this.props.body}</Text>
{this.props.children}
<BlockButton
backgroundColor={"#F0AC8F"}
borderColor={"#F0AC8F"}
fontColor={"white"}
title={this.props.buttonText}
padding={23}
marginTop={25}
disabled={false}
onPress={this.props.onClosed}
/>
</View>
</View>
</Modal>
);
} else {
return <View />;
}
}
}
BlurAlert.propTypes = {
title: String,
body: String,
buttonText: String,
visible: Boolean
};
BlurAlert.defaultProps = {
visible: false,
buttonText: "OK"
};
const styles = StyleSheet.create({
wrapper: {
position: "absolute",
top: 0,
left: 0,
bottom: 0,
right: 0,
height: "100%",
width: "100%",
zIndex: 9999
},
blurView: {
flex: 1,
zIndex: 99999
},
container: {
margin: 25,
zIndex: 99999,
position: "relative",
top: 0,
left: 0,
bottom: 0,
right: 0
},
box: {
backgroundColor: "white",
padding: 20,
position: "absolute",
top: "15%",
borderColor: "black",
borderWidth: StyleSheet.hairlineWidth,
margin: 25,
alignItems: "center",
shadowColor: "#000",
shadowOffset: { width: 0, height: 1 },
shadowOpacity: 0.1,
shadowRadius: 3,
zIndex: 99999,
width: "85%"
},
titleText: {
fontFamily: "Lora",
fontSize: 20
},
bodyText: {
marginTop: 15,
textAlign: "center",
fontFamily: "Lora",
fontSize: 16,
lineHeight: 20
}
});
I have some problems with AsyncStorage and i was hoping that you can help me out. I have to write some notes and save them so when I open the app again, they will be displayed on a ListView.
Here are the classes I used:
Notes.js:
import React, { Component } from 'react';
import {
View,
Text,
StyleSheet,
TouchableOpacity,
} from 'react-native';
export default class Note extends Component {
render() {
return (
<View key={this.props.keyval} style={styles.note}>
<Text style={styles.noteText}>{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>
);
}
}
const styles = StyleSheet.create({
note: {
position: 'relative',
padding: 20,
paddingRight: 100,
borderBottomWidth:2,
borderBottomColor: '#ededed'
},
noteText: {
paddingLeft: 20,
borderLeftWidth: 10,
borderLeftColor: '#0000FF'
},
noteDelete: {
position: 'absolute',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#2980b9',
padding: 10,
top: 10,
bottom: 10,
right: 10
},
noteDeleteText: {
color: 'white'
}
});
This is the component that I use every time when I want to create a note.
Main.js:
import React, { Component } from 'react';
import {
View,
Text,
StyleSheet,
TextInput,
ScrollView,
TouchableOpacity,
AsyncStorage,
} from 'react-native';
import Note from './Note';
export default class Main extends Component {
constructor(props){
super(props);
this.state = {
noteArray: [],
noteText: '',
};
this.getSavedNotes(this.state.noteArray);
}
render() {
let notes = this.state.noteArray.map((val, key)=>{
return <Note key={key} keyval={key} val={val}
deleteMethod={()=>this.deleteNote(key)}/>
});
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.headerText}>- NOTER -</Text>
</View>
<ScrollView style={styles.scrollContainer}>
{notes}
</ScrollView>
<View style={styles.footer}>
<TextInput
style={styles.textInput}
placeholder='Write your note here'
onChangeText={(noteText)=> this.setState({noteText})}
value={this.state.noteText}
placeholderTextColor='white'
underlineColorAndroid='transparent'>
</TextInput>
</View>
<TouchableOpacity onPress={ this.addNote.bind(this) } style={styles.addButton}>
<Text style={styles.addButtonText}>+</Text>
</TouchableOpacity>
</View>
);
}
addNote(){
if(this.state.noteText){
var d = new Date();
this.state.noteArray.push({
'date':d.getFullYear()+
"/"+(d.getMonth()+1) +
"/"+ d.getDate(),
'note': this.state.noteText
});
this.setState({ noteArray: this.state.noteArray });
this.setState({noteText:''});
AsyncStorage.setItem('arr', JSON.stringify(this.state.noteArray));
alert(this.state.noteArray);
}
}
deleteNote(key){
this.state.noteArray.splice(key, 1);
this.setState({noteArray: this.state.noteArray});
}
getSavedNotes = async (noteArray) =>{
try{
let data = await AsyncStorage.getItem('arr');
if(JSON.parse(data))
{
this.state.noteArray = JSON.parse(data);v
}
}catch(error){
alert(error);
}
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
header: {
backgroundColor: '#1A237E',
alignItems: 'center',
justifyContent:'center',
borderBottomWidth: 10,
borderBottomColor: '#ddd'
},
headerText: {
color: 'white',
fontSize: 18,
padding: 26
},
scrollContainer: {
flex: 1,
marginBottom: 100
},
footer: {
position: 'absolute',
bottom: 0,
backgroundColor: '#000000',
left: 0,
right: 70,
zIndex: 10
},
textInput: {
alignSelf: 'stretch',
color: '#fff',
padding: 20,
backgroundColor: '#252525',
borderTopWidth:2,
borderTopColor: '#ededed'
},
addButton: {
position: 'absolute',
zIndex: 11,
right: 0,
bottom: 0,
backgroundColor: '#1A237E',
width: 70,
height: 68,
// borderRadius: 35,
alignItems: 'center',
justifyContent: 'center',
elevation: 8
},
addButtonText: {
color: '#fff',
fontSize: 24
}
});
Here is where I try to save the notes. It seems like the notes are saved but they are not displayed as I open the app. They are displayed only after I tape a letter in the text box.
Finally the App.js:
import React, { Component } from 'react';
import Main from './app/components/Main.js';
export default class App extends Component {
render() {
return (
<Main/>
);
}
}
Here I just display the Main.js component.
getSavedNotes = async (noteArray) =>{
try{
let data = await AsyncStorage.getItem('arr');
data = JSON.parse(data);
this.setState({noteArray:data}) //assuming data as array of notes
}catch(error){
alert(error);
}
}
I have another problem with a button. I have to position it inside a ListView under the last item.
Here are the classes I used:
Notes.js:
import React, { Component } from 'react';
import {
View,
Text,
StyleSheet,
TouchableOpacity,
} from 'react-native';
export default class Note extends Component {
render() {
return (
<View key={this.props.keyval} style={styles.note}>
<Text style={styles.noteText}>{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>
);
}
}
const styles = StyleSheet.create({
note: {
position: 'relative',
padding: 20,
paddingRight: 100,
borderBottomWidth:2,
borderBottomColor: '#ededed'
},
noteText: {
paddingLeft: 20,
borderLeftWidth: 10,
borderLeftColor: '#0000FF'
},
noteDelete: {
position: 'absolute',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#2980b9',
padding: 10,
top: 10,
bottom: 10,
right: 10
},
noteDeleteText: {
color: 'white'
}
});
This is the component that I use every time when I want to create a note.
Main.js:
import React, { Component } from 'react';
import {
View,
Text,
StyleSheet,
TextInput,
ScrollView,
TouchableOpacity,
AsyncStorage,
} from 'react-native';
import Note from './Note';
export default class Main extends Component {
constructor(props){
super(props);
this.state = {
noteArray: [],
noteText: '',
};
this.getSavedNotes(this.state.noteArray);
}
render() {
let notes = this.state.noteArray.map((val, key)=>{
return <Note key={key} keyval={key} val={val}
deleteMethod={()=>this.deleteNote(key)}/>
});
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.headerText}>- NOTER -</Text>
</View>
<ScrollView style={styles.scrollContainer}>
{notes}
<TouchableOpacity onPress={ this.addNote.bind(this) } style={styles.addButton}>
<Text style={styles.addButtonText}>+</Text>
</TouchableOpacity>
</ScrollView>
<View style={styles.footer}>
<TextInput
style={styles.textInput}
placeholder='Write your note here'
onChangeText={(noteText)=> this.setState({noteText})}
value={this.state.noteText}
placeholderTextColor='white'
underlineColorAndroid='transparent'>
</TextInput>
</View>
</View>
);
}
addNote(){
if(this.state.noteText){
var d = new Date();
this.state.noteArray.push({
'date':d.getFullYear()+
"/"+(d.getMonth()+1) +
"/"+ d.getDate(),
'note': this.state.noteText
});
this.setState({ noteArray: this.state.noteArray });
this.setState({noteText:''});
AsyncStorage.setItem('arr', JSON.stringify(this.state.noteArray));
alert(this.state.noteArray);
}
}
deleteNote(key){
this.state.noteArray.splice(key, 1);
this.setState({noteArray: this.state.noteArray});
}
getSavedNotes = async (noteArray) =>{
try{
let data = await AsyncStorage.getItem('arr');
if(JSON.parse(data))
{
this.state.noteArray = JSON.parse(data);v
}
}catch(error){
alert(error);
}
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
header: {
backgroundColor: '#1A237E',
alignItems: 'center',
justifyContent:'center',
borderBottomWidth: 10,
borderBottomColor: '#ddd'
},
headerText: {
color: 'white',
fontSize: 18,
padding: 26
},
scrollContainer: {
flex: 1,
marginBottom: 100
},
footer: {
position: 'absolute',
bottom: 0,
backgroundColor: '#000000',
left: 0,
right: 70,
zIndex: 10
},
textInput: {
alignSelf: 'stretch',
color: '#fff',
padding: 20,
backgroundColor: '#252525',
borderTopWidth:2,
borderTopColor: '#ededed'
},
addButton: {
position: 'absolute',
zIndex: 11,
right: 0,
bottom: 0,
backgroundColor: '#1A237E',
width: 70,
height: 68,
// borderRadius: 35,
alignItems: 'center',
justifyContent: 'center',
elevation: 8
},
addButtonText: {
color: '#fff',
fontSize: 24
}
});
Here is where I save the notes and display them inside the ListView. After the insertion, the button should appear under the new added note.
Finally the App.js:
import React, { Component } from 'react';
import Main from './app/components/Main.js';
export default class App extends Component {
render() {
return (
<Main/>
);
}
}
Here I just display the Main.js component.
I made it! Here are the changes I made in Main.js file:
<ScrollView style={styles.scrollViewContainer}>
<ScrollView style={styles.scrollContainer}>
{notes}
</ScrollView>
<TouchableOpacity onPress={ this.addNote.bind(this) } style={styles.addButton}>
<Text style={styles.addButtonText}>+</Text>
</TouchableOpacity>
</ScrollView>
And here is the designSheet:
const styles = StyleSheet.create({
container: {
flex: 1,
},
header: {
backgroundColor: '#1A237E',
alignItems: 'center',
justifyContent:'center',
borderBottomWidth: 10,
borderBottomColor: '#ddd',
},
headerText: {
color: 'white',
fontSize: 18,
padding: 26
},
scrollContainer: {
flex: 1,
},
footer: {
position: 'absolute',
bottom: 0,
height: 70,
backgroundColor: '#000000',
left: 0,
right:0,
zIndex: 10,
},
textInput: {
alignSelf: 'stretch',
color: '#fff',
padding: 20,
backgroundColor: '#252525',
borderTopWidth:2,
borderTopColor: '#ededed',
marginRight: 70,
},
addButton: {
position: 'relative',
zIndex: 11,
left: 0,
top: 0,
backgroundColor: '#1A237E',
width: 70,
height: 70,
alignItems: 'center',
justifyContent: 'center',
elevation: 8
},
addButtonText: {
color: '#fff',
fontSize: 60
},
scrollViewContainer: {
flex: 1,
marginBottom: 70,
}
});
I hope this help others aswell