Advertisement
thanhnb

react redux:

Aug 27th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. react redux: state, reducer, store
  2. - state
  3. - reducer: combineReducers from 'redux': để tạo reducer khi mình tách ra nhiều reducer con
  4. const reducer = combineReducers({
  5. state1: reducer1,
  6. state2: reducer2,
  7. ....
  8. });
  9. - store: createStore from 'redux'
  10. const store = createStore(reducer);
  11. - actionCreator: tạo ra các function trả về 1 object js action
  12. export function action1(){
  13. return{
  14. type: 'action1',
  15. data1: 'test',
  16. ...
  17. }
  18. }
  19. - mapStateToProps: connect những thuộc tính của state store để sử dụng trong component hiện tại
  20. - mapActionToProps: connect những actionCreator để sử dụng trong component hiện tại. chỉ cần gọi this.props.action1 thay vì gọi this.props.dispatch(action1)....
  21. - redux-thunk: yarn add redux-thunk //xử lý bất đồng bộ
  22. const store = createStore(reducer, applyMiddleware
  23. tạo actionCreator của redux-thunk: là 1 function trả về 1 function
  24. export function fetchData(input){
  25. return dispatch=>{
  26. //code xử lý bất đồng bộ như việc fetch data từ api
  27. };
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement