Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用react-redux连接mapStateToProps、MapDispatchToProps和redux路由器_Javascript_Reactjs_Redux - Fatal编程技术网

Javascript 如何使用react-redux连接mapStateToProps、MapDispatchToProps和redux路由器

Javascript 如何使用react-redux连接mapStateToProps、MapDispatchToProps和redux路由器,javascript,reactjs,redux,Javascript,Reactjs,Redux,我想在容器中使用“登录页面”(智能组件)在登录后重定向。 大概是这样的: handleSubmit(username, pass, nextPath) { function redirect() { this.props.pushState(null, nextPath); } this.props.login(username, pass, redirect); //action from LoginAcitons } 用户名和密码来自哑组件 智能

我想在容器中使用“登录页面”(智能组件)在登录后重定向。 大概是这样的:

handleSubmit(username, pass, nextPath) {
    function redirect() {
        this.props.pushState(null, nextPath);
    }
    this.props.login(username, pass, redirect); //action from LoginAcitons
  }
用户名和密码来自哑组件

智能组件连接

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

function mapDispatchToProps(dispatch) {
  return bindActionCreators(LoginActions, dispatch)
}
如何从redux路由器添加pushState?还是我走错了路

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
简单骨架:

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'));
从“React”导入React;
从“react dom”导入react dom
从“redux”导入{createStore、applyMiddleware、CombineReducer}
从“react redux”导入{connect,Provider}
从“redux thunk”导入thunk
从“redux logger”导入记录器
从“./View”导入视图;
从“/reducers”导入{playlyReducer,artistReducers}
/*创建rootReducer*/
const rootReducer=combinereducer({
播放列表:播放列表,
艺术家:艺术家
})
/*创建存储*/
let store=createStore(rootReducer,applyMiddleware(logger,thunk));
/*连接视图和存储*/
const App=connect(
状态=>({
//与组合减速机相同的密钥
playlist:state.playlist,
艺术家:国家艺术家
}),
分派=>({
})
)(视图);
ReactDOM.render(
,
document.getElementById('wrapper');

这里的关键是要认识到,在MapStateTrops中,“状态”不仅仅是对象的本地状态,而是redux应用程序的单一状态。我认为倾向于认为您可以只使用return{user:state.user},因为最有可能的{user:''}是您在该组件的操作、减缩器等的本地得到的内容。@netpothina谢谢!这在redux文档中没有得到充分强调,我对同样的事情感到困惑。当然,我可能浏览得太快了,我也陷入了类似的问题。这里的
登录操作是什么?为什么需要在组件本身中完成?只是好奇。
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'));