gpt4 book ai didi

javascript - Redux 路由器 - "Dispatch is not defined"

转载 作者:可可西里 更新时间:2023-11-01 02:24:05 25 4
gpt4 key购买 nike

我有一个简单的组件,它在用户加载页面时调用一个操作,在该操作中,我试图分派(dispatch)另一个操作以将商店的 loggedIn 状态设置为真还是假:

import React, { Component } from 'react'
import { Link, browserHistory } from 'react-router'
import $ from 'jquery'

class Login extends Component {

constructor(props) {
super(props)
}
componentDidMount() {
this.props.actions.guestLoginRequest()
}
render() {
return (
<div>
<div classNameName="container">
<div className="row">
We are signing you in as a guest
</div>
</div>
</div>
)
}
}

export default Login

guestLoginRequest 操作被调用时,我可以获得登录信息,但是当我尝试在其中调度另一个操作时,没有任何反应:

guestLoginRequest: function(){
var ref = new Firebase("https://penguinradio.firebaseio.com");
ref.authAnonymously(function(error, authData) {
if (error) {
console.log("Login Failed!", error);
} else {
console.log("Authenticated successfully with payload:", authData);
return dispatch => {
dispatch(actions.setLoginStatus(true, authData))
console.log("dispatched");
};
}
});
}

当我删除 return dispatch => { } 语句时,我得到了 Uncaught ReferenceError: dispatch is not defined 错误。在我的商店中,我使用的是 redux-thunk,因此我可以在操作内部进行分派(dispatch):

// Store.js
import { applyMiddleware, compose, createStore } from 'redux'
import rootReducer from './reducers'
import logger from 'redux-logger'
import thunk from 'redux-thunk'

let finalCreateStore = compose(
applyMiddleware(thunk, logger())
)(createStore)


export default function configureStore(initialState = { loggedIn: false }) {
return finalCreateStore(rootReducer, initialState)
}

我也在我的 app.js 中将调度映射到 Prop :

function mapStateToProps(state) {
return state
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(actions, dispatch)
}
}

export default connect(mapStateToProps, mapDispatchToProps)(App)

为了以防万一,这里是我的 client.js 和 reducer 文件:

// client.js
import React from 'react'
import { render } from 'react-dom'
import App from '../components/App'
import configureStore from '../redux/store'
import { Provider } from 'react-redux'


let initialState = {
loggedIn: false
}

let store = configureStore(initialState)

render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('app')
)
// Reducer.js
import { combineReducers } from 'redux'

let LoginStatusReducer = function reducer(loggedIn = false, action) {
switch (action.type) {
case 'UPDATE_LOGIN_STATUS':
return loggedIn = action.boolean
default:
return loggedIn
}
}
export default LoginStatusReducer

const rootReducer = combineReducers({
loggedIn: LoginStatusReducer
})

export default rootReducer

知道为什么我的调度函数不起作用吗?我很困惑,因为我确实为我的商店设置了 redux-thunk,并且我在调用 return dispatch => {} 时使用的代码类似于文档。有什么我想念的吗?预先感谢您的任何建议!

最佳答案

你需要你的 Action 返回一个函数来使用 thunk 中间件,然后 redux 会把调度器注入(inject)它。您将调度程序调用与实现细节混合在一起。以下代码段修复了这两个缺陷。

guestLoginRequest: function(){
return function (dispatch) {
var ref = new Firebase("https://penguinradio.firebaseio.com");
ref.authAnonymously(function(error, authData) {
if (error) {
console.log("Login Failed!", error);
} else {
console.log("Authenticated successfully with payload:", authData);
dispatch(actions.setLoginStatus(true, authData))
console.log("dispatched");
}
});
}
}

此外,您需要在 Login 类上正确分派(dispatch)操作。

dispatch(this.props.actions.guestLoginRequest())

您的操作调用始终通过调用 dispatch 来完成。流程应该是这样的:

React component --> dispatch ---> API call (thunk middleware) --> dispatch ---> reducer

关于javascript - Redux 路由器 - "Dispatch is not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36462284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com