2020/02/09
npx create-react-native-app 폴더명
AsyncStorage
브라우저의 local,sessionStorage와 같은 역할을 한다.import React, { Component } from 'react';
import { StyleSheet, Text, View, AsyncStorage, Button } from 'react-native';
export default class App extends Component {
click = () => {
const newNumber = Math.random();
this.setState({
thanosNumber: newNumber,
});
AsyncStorage.setItem('thanosNumber', newNumber.toString());
};
excute = async () => {
const result = await AsyncStorage.getItem('thanosNumber');
if (result) {
this.setState({
thanosNumber: Number(result),
});
} else {
this.click();
}
};
constructor() {
super();
this.state = {
thanosNumber: null,
};
this.excute();
}
result = () => {
const { thanosNumber } = this.state;
let resultText = '';
if (thanosNumber === null) {
resultText = '';
} else if (thanosNumber < 0.5) {
resultText = '우주의 균형을 위해 먼지가....';
} else {
resultText = '당신은 살아남았습니다!!';
}
return <Text style={styles.text}>{resultText}</Text>;
};
render() {
return (
<View style={styles.container}>
{this.result()}
<Button title={'Click~~here~'} onPress={this.click} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
text: {
color: '#27ae60',
fontSize: 26,
fontWeight: 'bold',
marginBottom: 30,
},
});