Skip to content

Instantly share code, notes, and snippets.

@melikhov-dev
Created October 27, 2017 19:35
Show Gist options
  • Save melikhov-dev/a81b4d008392ad5c982f42de1bb7d06c to your computer and use it in GitHub Desktop.
Save melikhov-dev/a81b4d008392ad5c982f42de1bb7d06c to your computer and use it in GitHub Desktop.
// Определяем состояние и методы для его обновления
function ReduxState (state = Immutable.fromJS({items: []}), action) {
switch (actions.type) {
case 'addItem':
return state.push('items', action.item)
}
return state
}
// Компоненты
const Items = connect(
(state) => {
return {
items: state.items
}
},
(dispatch) => {
return {
onClick: (item) => {
dispatch({type: 'addItem', item})
}
}
}
)(
function ItemsComponent ({items, onClick}) {
return (
<div>
<ul>
{items.map((item) => <li>{item}</li>)}
</ul>
<button onClick={() => onClick('foo')}>
Add foo
</button>
</div>
)
}
)
// Передаём состояние в компоненты
const store = createStore(ReduxState)
render(
<Provider store={store}>
<Items />
</Provider>,
document.getElementById('root')
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment