1- import React from 'react'
1+ import React , { useEffect } from 'react'
22import { Platform , Text , View , Button , ActivityIndicator , Image } from 'react-native'
3- import { connect } from 'react-redux'
4- import { PropTypes } from 'prop-types'
3+ import { useDispatch , useSelector } from 'react-redux'
54import ExampleActions from 'App/Stores/Example/Actions'
65import { liveInEurope } from 'App/Stores/Example/Selectors'
76import Style from './ExampleScreenStyle'
@@ -13,85 +12,62 @@ import { ApplicationStyles, Helpers, Images, Metrics } from 'App/Theme'
1312 * This screen displays a little help message and informations about a fake user.
1413 * Feel free to remove it.
1514 */
16-
1715const instructions = Platform . select ( {
1816 ios : 'Press Cmd+R to reload,\nCmd+D or shake for dev menu.' ,
1917 android : 'Double tap R on your keyboard to reload,\nShake or press menu button for dev menu.' ,
2018} )
2119
22- class ExampleScreen extends React . Component {
23- componentDidMount ( ) {
24- this . _fetchUser ( )
25- }
20+ const ExampleScreen = ( ) => {
21+ const dispatch = useDispatch ( )
2622
27- render ( ) {
28- return (
29- < View
30- style = { [
31- Helpers . fill ,
32- Helpers . rowMain ,
33- Metrics . mediumHorizontalMargin ,
34- Metrics . mediumVerticalMargin ,
35- ] }
36- >
37- { this . props . userIsLoading ? (
38- < ActivityIndicator size = "large" color = "#0000ff" />
39- ) : (
40- < View >
41- < View style = { Style . logoContainer } >
42- < Image style = { Helpers . fullSize } source = { Images . logo } resizeMode = { 'contain' } />
43- </ View >
44- < Text style = { Style . text } > To get started, edit App.js</ Text >
45- < Text style = { Style . instructions } > { instructions } </ Text >
46- { this . props . userErrorMessage ? (
47- < Text style = { Style . error } > { this . props . userErrorMessage } </ Text >
48- ) : (
49- < View >
50- < Text style = { Style . result } >
51- { "I'm a fake user, my name is " }
52- { this . props . user . name }
53- </ Text >
54- < Text style = { Style . result } >
55- { this . props . liveInEurope ? 'I live in Europe !' : "I don't live in Europe." }
56- </ Text >
57- </ View >
58- ) }
59- < Button
60- style = { ApplicationStyles . button }
61- onPress = { ( ) => this . _fetchUser ( ) }
62- title = "Refresh"
63- />
64- </ View >
65- ) }
66- </ View >
67- )
68- }
23+ const user = useSelector ( ( state ) => state . example . user )
24+ const userIsLoading = useSelector ( ( state ) => state . example . userIsLoading )
25+ const userErrorMessage = useSelector ( ( state ) => state . example . userErrorMessage )
6926
70- _fetchUser ( ) {
71- this . props . fetchUser ( )
72- }
73- }
27+ useEffect ( ( ) => {
28+ dispatch ( ExampleActions . fetchUser ( ) )
29+ } , [ ] )
7430
75- ExampleScreen . propTypes = {
76- user : PropTypes . object ,
77- userIsLoading : PropTypes . bool ,
78- userErrorMessage : PropTypes . string ,
79- fetchUser : PropTypes . func ,
80- liveInEurope : PropTypes . bool ,
31+ return (
32+ < View
33+ style = { [
34+ Helpers . fillCenter ,
35+ Helpers . rowMain ,
36+ Metrics . mediumHorizontalMargin ,
37+ Metrics . mediumVerticalMargin ,
38+ ] }
39+ >
40+ { userIsLoading ? (
41+ < ActivityIndicator size = "large" color = "#0000ff" />
42+ ) : (
43+ < View >
44+ < View style = { Style . logoContainer } >
45+ < Image style = { Helpers . fullSize } source = { Images . logo } resizeMode = { 'contain' } />
46+ </ View >
47+ < Text style = { Style . text } > To get started, edit App.js</ Text >
48+ < Text style = { Style . instructions } > { instructions } </ Text >
49+ { userErrorMessage ? (
50+ < Text style = { Style . error } > { userErrorMessage } </ Text >
51+ ) : (
52+ < View >
53+ < Text style = { Style . result } >
54+ { "I'm a fake user, my name is " }
55+ { user . name }
56+ </ Text >
57+ < Text style = { Style . result } >
58+ { liveInEurope ? 'I live in Europe !' : "I don't live in Europe." }
59+ </ Text >
60+ </ View >
61+ ) }
62+ < Button
63+ style = { ApplicationStyles . button }
64+ onPress = { ( ) => dispatch ( ExampleActions . fetchUser ( ) ) }
65+ title = "Refresh"
66+ />
67+ </ View >
68+ ) }
69+ </ View >
70+ )
8171}
8272
83- const mapStateToProps = ( state ) => ( {
84- user : state . example . user ,
85- userIsLoading : state . example . userIsLoading ,
86- userErrorMessage : state . example . userErrorMessage ,
87- liveInEurope : liveInEurope ( state ) ,
88- } )
89-
90- const mapDispatchToProps = ( dispatch ) => ( {
91- fetchUser : ( ) => dispatch ( ExampleActions . fetchUser ( ) ) ,
92- } )
93-
94- export default connect (
95- mapStateToProps ,
96- mapDispatchToProps
97- ) ( ExampleScreen )
73+ export default ExampleScreen
0 commit comments