복붙노트

[REACTJS] mapStateToProps, MapDispatchToProps 및 REDUX 라우터에 연결하는 방법을 사용 반응-REDUX

REACTJS

mapStateToProps, MapDispatchToProps 및 REDUX 라우터에 연결하는 방법을 사용 반응-REDUX

해결법


  1. 1.

    function mapStateToProps(state) {
      return {
        user: state.app.user
      };
    }
    
    function mapDispatchToProps(dispatch) {
      return {
        actions: bindActionCreators(LoginActions, dispatch),
        routerActions: bindActionCreators({pushState}, dispatch)
      }
    }
    
    export default connect(mapStateToProps, mapDispatchToProps)(LoginPage);
    

  2. 2.간단한 골격 :

    간단한 골격 :

    import React from 'react';
    import ReactDOM from 'react-dom'
    import { createStore,applyMiddleware, combineReducers } from 'redux'
    import { connect, Provider } from 'react-redux'
    import thunk from 'redux-thunk'
    import logger from 'redux-logger'
    
    import View from './view';
    import {playListReducer, artistReducers} from './reducers'
    
    
    /*create rootReducer*/
    
    
    const rootReducer = combineReducers({
      playlist: playListReducer,
      artist: artistReducers
    })
    
    /* create store */
    let store = createStore(rootReducer,applyMiddleware(logger ,thunk));
    
    
    /*  connect view and store   */
    const App = connect(
      state => ({
        //same key as combineReducers
        playlist:state.playlist,
        artist:state.artist
      }),
      dispatch => ({
    
        })
      )(View);
    
    
    
    ReactDOM.render(
      <Provider store={store}>
      <App />
      </Provider>  ,
      document.getElementById('wrapper'));
    
  3. from https://stackoverflow.com/questions/34450396/how-use-react-redux-connect-with-mapstatetoprops-mapdispatchtoprops-and-redux-ro by cc-by-sa and MIT license