2015-12-24 25 views
29

Tôi muốn sử dụng trong chuyển hướng "LoginPage" vùng chứa (thành phần thông minh) của tôi sau khi đăng nhập. Một cái gì đó như thế này:Cách sử dụng phản ứng-redux kết nối với mapStateToProps, MapDispatchToProps và redux-router

handleSubmit(username, pass, nextPath) { 
    function redirect() { 
     this.props.pushState(null, nextPath); 
    } 
    this.props.login(username, pass, redirect); //action from LoginAcitons 
    } 

tên người dùng và vượt qua đến từ thành phần câm.

thành phần thông minh kết nối

function mapStateToProps(state) { 
    return { 
    user: state.app.user 
    }; 
} 

function mapDispatchToProps(dispatch) { 
    return bindActionCreators(LoginActions, dispatch) 
} 

Làm thế nào tôi có thể thêm pushState từ Redux-router? Hay tôi đang đi sai đường?

export default connect(mapStateToProps, {pushState})(LoginPage); //works, but haven't actions 

export default connect(mapStateToProps, mapDispatchToProps)(LoginPage); //works, but haven't pushState 

export default connect(mapStateToProps, mapDispatchToProps, {pushState})(LoginPage); //Uncaught TypeError: finalMergeProps is not a function 
+0

Tôi đang mắc kẹt trong một vấn đề tương tự. 'Hành động đăng nhập' ở đây là gì? –

+0

tại sao điều này cần được thực hiện trong chính cấu phần? Chỉ tò mò thôi. – xckpd7

Trả lời

26
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); 
+24

Điều quan trọng ở đây là nhận ra rằng trong mapStateToProps, "trạng thái" không chỉ là trạng thái cục bộ của đối tượng, mà là trạng thái đơn của ứng dụng redux. Tôi nghĩ xu hướng đó là nghĩ rằng bạn chỉ có thể sử dụng return {user: state.user} vì hầu hết {user: ''} là những gì bạn có cục bộ đối với các hành động của thành phần đó, bộ giảm tốc, v.v. – netpoetica

+0

@netpoetica Cảm ơn bạn! Điều này được nhấn mạnh trong tài liệu redux, tôi đã nhầm lẫn về điều tương tự. Cấp, tôi có thể lướt qua nó quá nhanh XD – pixelpax

0

Simple xương:

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')); 
Các vấn đề liên quan