8
8
*/
9
9
10
10
import React , { Component } from 'react' ;
11
- import { Platform , StyleSheet , Text , View } from 'react-native' ;
11
+ import { Platform , StyleSheet , Button , Text , TextInput , View } from 'react-native' ;
12
12
13
13
import RNSecureKeyStore , { ACCESSIBLE } from "react-native-secure-key-store" ;
14
14
15
- const instructions = Platform . select ( {
16
- ios : 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu' ,
17
- android :
18
- 'Double tap R on your keyboard to reload,\n' +
19
- 'Shake or press menu button for dev menu' ,
20
- } ) ;
21
-
22
15
type Props = { } ;
23
16
export default class App extends Component < Props > {
24
- render ( ) {
17
+ state = {
18
+ alias : 'hello' ,
19
+ value : 'world'
20
+ } ;
25
21
26
- RNSecureKeyStore . set ( "key1" , "value1" , { accessible : ACCESSIBLE . ALWAYS_THIS_DEVICE_ONLY } )
27
- . then ( ( res ) => {
28
- console . log ( res ) ;
29
- } , ( err ) => {
30
- console . log ( err ) ;
31
- } ) ;
22
+ getValue ( ) {
23
+ RNSecureKeyStore . get ( this . state . alias )
24
+ . then ( ( value ) => {
25
+ this . setState ( {
26
+ value,
27
+ } ) ;
28
+ } )
29
+ . catch ( console . error ) ;
30
+ }
32
31
33
- RNSecureKeyStore . set ( "key2" , "value2" , { accessible : ACCESSIBLE . ALWAYS_THIS_DEVICE_ONLY } )
34
- . then ( ( res ) => {
35
- console . log ( res ) ;
36
- } , ( err ) => {
37
- console . log ( err ) ;
38
- } ) ;
32
+ setValue ( ) {
33
+ RNSecureKeyStore . set ( this . state . alias , this . state . value , { } )
34
+ . then ( ( ) => this . getValue ( ) )
35
+ . catch ( console . error ) ;
36
+ }
39
37
40
- RNSecureKeyStore . get ( "key1" )
41
- . then ( ( res ) => {
42
- console . log ( res ) ;
43
- } , ( err ) => {
44
- console . log ( err ) ;
45
- } ) ;
38
+ removeValue ( ) {
39
+ RNSecureKeyStore . remove ( this . state . alias )
40
+ . then ( ( ) => this . getValue ( ) )
41
+ . catch ( console . error ) ;
42
+ }
46
43
47
- RNSecureKeyStore . get ( "key2" )
48
- . then ( ( res ) => {
49
- console . log ( res ) ;
50
- } , ( err ) => {
51
- console . log ( err ) ;
52
- } ) ;
44
+ render ( ) {
45
+ return (
46
+ < View style = { styles . container } >
47
+ < View style = { styles . row } >
48
+ < Text > Alias:</ Text >
49
+ < TextInput
50
+ style = { styles . textInput }
51
+ onChangeText = { alias => this . setState ( { alias} ) }
52
+ value = { this . state . alias }
53
+ />
54
+ </ View >
53
55
54
- RNSecureKeyStore . remove ( "key1" )
55
- . then ( ( res ) => {
56
- console . log ( res ) ;
57
- } , ( err ) => {
58
- console . log ( err ) ;
59
- } ) ;
56
+ < View style = { styles . row } >
57
+ < Text > Value:</ Text >
58
+ < TextInput
59
+ style = { styles . textInput }
60
+ onChangeText = { value => this . setState ( { value} ) }
61
+ value = { this . state . value }
62
+ />
63
+ </ View >
60
64
61
- RNSecureKeyStore . remove ( "key2" )
62
- . then ( ( res ) => {
63
- console . log ( res ) ;
64
- } , ( err ) => {
65
- console . log ( err ) ;
66
- } ) ;
65
+ < View style = { styles . row } >
66
+ < View style = { styles . button } >
67
+ < Button
68
+ onPress = { ( ) => this . getValue ( ) }
69
+ title = 'Get'
70
+ />
71
+ </ View >
72
+ < View style = { styles . button } >
73
+ < Button
74
+ onPress = { ( ) => this . setValue ( ) }
75
+ title = 'Set'
76
+ />
77
+ </ View >
78
+ < View style = { styles . button } >
79
+ < Button
80
+ onPress = { ( ) => this . removeValue ( ) }
81
+ title = 'Remove'
82
+ />
83
+ </ View >
84
+ </ View >
67
85
68
- return (
69
- < View style = { styles . container } >
70
- < Text style = { styles . welcome } > Welcome to React Native!</ Text >
71
- < Text style = { styles . instructions } > To get started, edit App.js</ Text >
72
- < Text style = { styles . instructions } > { instructions } </ Text >
73
86
</ View >
74
87
) ;
75
88
}
@@ -82,14 +95,20 @@ const styles = StyleSheet.create({
82
95
alignItems : 'center' ,
83
96
backgroundColor : '#F5FCFF' ,
84
97
} ,
85
- welcome : {
86
- fontSize : 20 ,
87
- textAlign : 'center' ,
88
- margin : 10 ,
98
+ row : {
99
+ flexDirection : 'row' ,
100
+ justifyContent : 'space-between' ,
101
+ alignItems : 'center' ,
102
+ marginTop : 20
89
103
} ,
90
- instructions : {
91
- textAlign : 'center' ,
92
- color : '#333333' ,
93
- marginBottom : 5 ,
104
+ textInput : {
105
+ height : 40 ,
106
+ width : 200 ,
107
+ borderColor : 'gray' ,
108
+ borderWidth : 1 ,
109
+ marginLeft : 10
94
110
} ,
111
+ button : {
112
+ marginLeft : 10
113
+ }
95
114
} ) ;
0 commit comments