diff --git a/App/Containers/Example/ExampleScreen.js b/App/Containers/Example/ExampleScreen.js index 19c300bc8..0af244313 100644 --- a/App/Containers/Example/ExampleScreen.js +++ b/App/Containers/Example/ExampleScreen.js @@ -3,6 +3,7 @@ import { Platform, Text, View, Button } from 'react-native' import { connect } from 'react-redux' import { PropTypes } from 'prop-types' import ExampleActions from 'App/Stores/Example/Actions' +import StartupActions from 'App/Stores/Startup/Actions' import { isHot } from 'App/Stores/Example/Selectors' import Style from './ExampleScreenStyle' @@ -13,7 +14,7 @@ const instructions = Platform.select({ class ExampleScreen extends React.Component { componentDidMount() { - this.props.fetchTemperature() + this.props.startup() } render() { @@ -49,6 +50,7 @@ const mapStateToProps = (state) => ({ }) const mapDispatchToProps = (dispatch) => ({ + startup: () => dispatch(StartupActions.startup()), fetchTemperature: () => dispatch(ExampleActions.fetchTemperature()), }) diff --git a/App/Sagas/StartupSaga.js b/App/Sagas/StartupSaga.js new file mode 100644 index 000000000..2e8c384fb --- /dev/null +++ b/App/Sagas/StartupSaga.js @@ -0,0 +1,11 @@ +import { put } from 'redux-saga/effects' +import ExampleActions from 'App/Stores/Example/Actions' + +/** + * The startup saga is the place to define behavior to execute when the application starts. + */ +export function* startup() { + // Dispatch a redux action using `put()` + // @see https://redux-saga.js.org/docs/basics/DispatchingActions.html + yield put(ExampleActions.fetchTemperature()) +} diff --git a/App/Sagas/index.js b/App/Sagas/index.js index 28348f01d..198086b81 100644 --- a/App/Sagas/index.js +++ b/App/Sagas/index.js @@ -1,12 +1,16 @@ import { takeLatest } from 'redux-saga/effects' import { ExampleTypes } from 'App/Stores/Example/Actions' +import { StartupTypes } from 'App/Stores/Startup/Actions' import { fetchTemperature } from './ExampleSaga' +import { startup } from './StartupSaga' export default function* root() { yield [ /** * @see https://redux-saga.js.org/docs/basics/UsingSagaHelpers.html */ + // Run the startup saga when the application starts + takeLatest(StartupTypes.STARTUP, startup), // Call `fetchTemperature()` when a `FETCH_TEMPERATURE` action is triggered takeLatest(ExampleTypes.FETCH_TEMPERATURE, fetchTemperature), ] diff --git a/App/Stores/Startup/Actions.js b/App/Stores/Startup/Actions.js new file mode 100644 index 000000000..91e248210 --- /dev/null +++ b/App/Stores/Startup/Actions.js @@ -0,0 +1,8 @@ +import { createActions } from 'reduxsauce' + +const { Types, Creators } = createActions({ + startup: null, +}) + +export const StartupTypes = Types +export default Creators