Skip to content

Commit

Permalink
Add selector example
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Napoli committed Aug 13, 2018
1 parent 3be6657 commit 89bb3d4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions App/Containers/HomeScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Platform, StyleSheet, Text, View, Button } from 'react-native'
import { connect } from 'react-redux'
import { PropTypes } from 'prop-types'
import ExampleActions from 'App/Stores/Example/Actions'
import { isHot } from 'App/Stores/Example/Selectors'

const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
Expand All @@ -23,6 +24,7 @@ class HomeScreen extends React.Component {
<Text style={styles.instructions}>
The weather temperature is: {this.props.temperature}
</Text>
<Text style={styles.instructions}>{this.props.isHot ? "It's pretty hot!" : ''}</Text>
<Text style={styles.instructions}>{this.props.errorMessage}</Text>
<Button onPress={this.props.fetchTemperature} title="Refresh" />
</View>
Expand Down Expand Up @@ -55,6 +57,7 @@ HomeScreen.propsTypes = {
const mapStateToProps = (state) => ({
temperature: state.example.get('temperature'),
errorMessage: state.example.get('errorMessage'),
isHot: isHot(state),
})

const mapDispatchToProps = (dispatch) => ({
Expand Down
11 changes: 11 additions & 0 deletions App/Stores/Example/Selectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Selectors let us factorize logic that queries the state.
*
* Selectors can be used in sagas or components to avoid duplicating that logic.
*
* Writing selectors is optional as it is not always necessary, we provide a simple example below.
*/

export const isHot = (state) => {
return state.example.get('temperature') && state.example.get('temperature') > 25
}

0 comments on commit 89bb3d4

Please sign in to comment.