Skip to content

Instantly share code, notes, and snippets.

@TechNinjaWeb
Created November 2, 2017 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TechNinjaWeb/f8dcb20a4c21c8fd7c57ebd0f0271bb8 to your computer and use it in GitHub Desktop.
Save TechNinjaWeb/f8dcb20a4c21c8fd7c57ebd0f0271bb8 to your computer and use it in GitHub Desktop.
Basic starter template for Redux with React-Native
import { AppRegistry, Text } from 'react-native'
import { createStore } from 'redux'
// Define action types
const types = {
INCREMENT: 'INCREMENT',
}
// Define a reducer
const reducer = (state, action) => {
if (action.type === types.INCREMENT) {
return {count: state.count + 1}
}
return state
}
// Define the initial state of our store
const initialState = {count: 0}
// Create a store, passing our reducer function and our initial state
const store = createStore(reducer, initialState)
/// We're done! Redux is all set up. Here's how we can use it:
// Now we can dispatch actions, which are understood by our store/reducer
store.dispatch({type: types.INCREMENT})
store.dispatch({type: types.INCREMENT})
store.dispatch({type: types.INCREMENT})
// Calling `store.getState()` returns our state object
const App = () => (
<Text style={{fontSize: 100}}>
{store.getState().count}
</Text>
)
AppRegistry.registerComponent('App', () => App)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment