Created
October 27, 2017 19:35
-
-
Save melikhov-dev/a81b4d008392ad5c982f42de1bb7d06c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Определяем состояние и методы для его обновления | |
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