Skip to content

Commit 89bb3d4

Browse files
author
Matthieu Napoli
committed
Add selector example
1 parent 3be6657 commit 89bb3d4

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

App/Containers/HomeScreen.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Platform, StyleSheet, Text, View, Button } from 'react-native'
33
import { connect } from 'react-redux'
44
import { PropTypes } from 'prop-types'
55
import ExampleActions from 'App/Stores/Example/Actions'
6+
import { isHot } from 'App/Stores/Example/Selectors'
67

78
const instructions = Platform.select({
89
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
@@ -23,6 +24,7 @@ class HomeScreen extends React.Component {
2324
<Text style={styles.instructions}>
2425
The weather temperature is: {this.props.temperature}
2526
</Text>
27+
<Text style={styles.instructions}>{this.props.isHot ? "It's pretty hot!" : ''}</Text>
2628
<Text style={styles.instructions}>{this.props.errorMessage}</Text>
2729
<Button onPress={this.props.fetchTemperature} title="Refresh" />
2830
</View>
@@ -55,6 +57,7 @@ HomeScreen.propsTypes = {
5557
const mapStateToProps = (state) => ({
5658
temperature: state.example.get('temperature'),
5759
errorMessage: state.example.get('errorMessage'),
60+
isHot: isHot(state),
5861
})
5962

6063
const mapDispatchToProps = (dispatch) => ({

App/Stores/Example/Selectors.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Selectors let us factorize logic that queries the state.
3+
*
4+
* Selectors can be used in sagas or components to avoid duplicating that logic.
5+
*
6+
* Writing selectors is optional as it is not always necessary, we provide a simple example below.
7+
*/
8+
9+
export const isHot = (state) => {
10+
return state.example.get('temperature') && state.example.get('temperature') > 25
11+
}

0 commit comments

Comments
 (0)