React_Native_Tutorial/App.js

91 lines
2.6 KiB
JavaScript
Raw Normal View History

2021-12-29 12:29:29 +01:00
import React, { Component } from 'react'; // Bei React bzw. Nativ immer importieren!
2021-12-28 15:50:53 +01:00
import { StatusBar } from 'expo-status-bar';
2021-12-29 14:06:22 +01:00
import { Button, StyleSheet, View } from 'react-native';
2021-12-28 15:50:53 +01:00
2022-01-04 18:05:37 +01:00
import Quote from './js/components/Quote'; // Eigene Componente
import NewQuote from './js/components/NewQuote';// Eigene Componente
2021-12-29 12:29:29 +01:00
const data = [
2022-01-04 12:23:46 +01:00
{ text: "Lernen ist Erfahrung. Alles andere ist einfach nur Information.", author: "Albert Einstein" },
2021-12-29 12:29:29 +01:00
{ text: "Zitat2", author: "Author 2" },
{ text: "Zitat3", author: "Author 3" },
{ text: "Zitat4", author: "Author 4" },
{ text: "Zitat5", author: "Author 5" },
];
export default class App extends Component {
2022-01-04 18:05:37 +01:00
state = { index: 0, showNewQuoteScreen: false }; //initialer Zustand
// Eigene Methoden mit unterstrich am Anfang
_addQuote = () => {
// NewQuote Ausblenden
this.setState({ showNewQuoteScreen: false });
}
2021-12-29 12:29:29 +01:00
// Darstellung der Komponente im UI
2021-12-29 14:06:22 +01:00
// Render wird automatisch ausgeführt:
2021-12-29 12:29:29 +01:00
// a) Komponente erscheint im Ui (initialer Zustand)
// b) Zustand ändert sich (state) => this.setstate(...)
2021-12-29 14:06:22 +01:00
// c) probs sich ändern
2021-12-29 12:29:29 +01:00
render() {
let index = this.state.index;
const quote = data[index];
let nextIndex = index + 1;
if (nextIndex === data.length) nextIndex = 0;
2021-12-29 14:06:22 +01:00
let prevIndex = index - 1;
if (index === 0) prevIndex = data.length - 1;
2021-12-29 12:29:29 +01:00
return (
//JSX
<View style={styles.container}>
2022-01-04 18:05:37 +01:00
<View style={styles.buttonNew}>
<Button
title="New"
onPress={() => this.setState({ showNewQuoteScreen: true })}>
</Button>
2022-01-04 12:34:49 +01:00
</View>
2022-01-04 18:05:37 +01:00
<NewQuote
visible={this.state.showNewQuoteScreen}
onSave={this._addQuote} />
<Quote
text={quote.text}
author={quote.author} />
<View style={styles.buttonNext}>
<Button
color='grey'
title="Nächstes Zitat"
onPress={() => this.setState({ index: nextIndex })} />
</View>
<View style={styles.buttonPrev}>
<Button
title="Letztes Zitat"
onPress={() => this.setState({ index: prevIndex })} />
2022-01-04 12:23:46 +01:00
</View>
2021-12-29 12:29:29 +01:00
<StatusBar style="auto" />
</View>
);
}
2021-12-28 15:50:53 +01:00
}
2021-12-29 12:29:29 +01:00
//Aussehen und Layout
2021-12-28 15:50:53 +01:00
const styles = StyleSheet.create({
container: {
flex: 1,
2022-01-04 12:23:46 +01:00
backgroundColor: '#F2F0FF',
2022-01-04 18:05:37 +01:00
alignItems: 'center', //FlexBox Layout
2021-12-28 15:50:53 +01:00
justifyContent: 'center',
2022-01-04 12:23:46 +01:00
},
2022-01-04 18:05:37 +01:00
buttonNext: {
2022-01-04 12:34:49 +01:00
position: 'absolute',
bottom: 50,
},
2022-01-04 18:05:37 +01:00
buttonPrev: {
2022-01-04 12:23:46 +01:00
position: 'absolute',
bottom: 5,
2021-12-28 15:50:53 +01:00
},
2022-01-04 18:05:37 +01:00
buttonNew: {
position: 'absolute',
right: 0,
top: 25,
}
2021-12-28 15:50:53 +01:00
});