Skip to content

Instantly share code, notes, and snippets.

@ngoryachev
Created January 14, 2020 19:16
Show Gist options
  • Save ngoryachev/c5879507ac98d76a645c30a8958d41f2 to your computer and use it in GitHub Desktop.
Save ngoryachev/c5879507ac98d76a645c30a8958d41f2 to your computer and use it in GitHub Desktop.
// store.js
import { createStore, combineReducers } from 'redux'
import {
users,
} from '../ducks/user'
export const initialState = {}
export const store = createStore(users, initialState)
// state = {
name,
age
}
// combinedStateExample state = {
user: { name, age },
logs: [ '22-01-2020', ... ],
}
// OEF
//users.js
// ACTIONS
export const setName(name) => ({ type: 'SET_NAME', payload: name })
// REDUCER
const users = (state = { name: "", age: 0 }, action) => {
if (action.type === 'SET_NAME') {
return {...state, name};
} else if {
return state;
}
}
export default users
// EOF
// index.js
// App.js
import {Provider} from 'react-redux'
import store from 'store.js'
const App = ({}) => (
<Provider store={store}>
<UserContainer />
</Provider>
)
// User.js
import {connect} from 'react-redux'
import {setName} from 'users.js'
const User = ({ name, age, setName }) => {
return
<View>
<Text>{name}<Text/>
<Text>{age}<Text/>
<Button onPress={() => setName('bro')} />
</View>
}
const mapStateToProps = (state) => ({ state.name, state.age })
const mapDispatchToProps = { setName: (name) => dispatch(({ type: 'SET_NAME', payload: name })) })
const UserContainer = connect(mapStateToProps, mapDispatchToProps)(User)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment