React-redux
安装
cnpm install –save react-redux
Provider
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import React from 'react'; import ReactDOM from 'react-dom'; import TodoList from './TodoList'; import { Provider } from 'react-redux'; import store from './store';
const App = ( <Provider store={ store }> <TodoList> </TodoList> </Provider> )
ReactDOM.render(App, document.getElementById('root'));
|
connect
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| import React from 'react'; import { connect } from 'react-redux'; import { changeInputAction, clickAddItemAction, clickDeleteItemAction } from './store/actionCreator'
const TodoList = (props) => { const { inputValue, list, onChangeInput, addItem, deleteItem } = props return( <div> <div> <input value={ inputValue } onChange= { onChangeInput } /> <button onClick={ addItem }> 提交 </button> </div> <ul> { list.map((item, index)=>{ return (<li key={ index } onClick= { () => deleteItem(index) }> { item } </li> ) }) } </ul> </div> ) }
const mapStateToProps = (state) => { return { inputValue: state.inputValue, list: state.list } }
const mapDispatchToProps = (dispatch) => { return{ onChangeInput(e){ const value = e.target.value; const action = changeInputAction(value); dispatch(action); },
addItem(){ const action = clickAddItemAction(); dispatch(action); },
deleteItem(index){ const action=clickDeleteItemAction(index); dispatch(action); } } }
export default connect(mapStateToProps, mapDispatchToProps)(TodoList);
|
https://github.com/rexyan/simple_react/tree/React-redux