How use react-redux connect with mapStateToProps,MapDispatchToProps and redux-router

前端 未结 2 488
既然无缘
既然无缘 2021-01-31 17:05

I want to use in my container \"LoginPage\" (smart-component) redirect after login. Something like this:

handleSubmit(username, pass, nextPath) {
    function re         


        
相关标签:
2条回答
  • 2021-01-31 17:31

    Simple skeleton :

    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'));
    
    0 讨论(0)
  • 2021-01-31 17:42
    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);
    
    0 讨论(0)
提交回复
热议问题