简体   繁体   English

applyMiddleware() 被忽略了? redux-thunk 从不触发回调,redux-logger 不会记录,一切都做对了

[英]applyMiddleware() ignored? redux-thunk never fires callback, redux-logger won't log, everything done right

I have no idea what's going on here, I am using this technique correctly in React Native but won't work in ReactJS我不知道这里发生了什么,我在 React Native 中正确使用了这种技术,但在 ReactJS 中不起作用

Console just logs 'Outside' and never 'Inside', also no redux log is provided.控制台只记录“外部”而不记录“内部”,也没有提供 redux 日志。

Application entry point:应用入口点:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import { Provider } from 'react-redux'
import { createStore, applyMiddleware, combineReducers } from 'redux'
import thunk from 'redux-thunk'
import logger from 'redux-logger'
import App from './App';
import registerServiceWorker from './registerServiceWorker';

import { authStateReducer } from './reducers/auth.js'
import { wishesStateReducer } from './reducers/wishes.js'

const root = combineReducers({ 
    auth: authStateReducer,
    wishes: wishesStateReducer
})

let store = createStore(root, applyMiddleware(thunk, logger))

ReactDOM.render(
    <Provider store={store}>
        <App />
    </Provider>, 
    document.getElementById('root')
);
registerServiceWorker();

Exporting root component like this:像这样导出根组件:

function stateToProps(state) {
  return { AUTH: state.auth, WISHES: state.wishes }
}

function dispatchToProps (dispatch) {
  return {
    registerAuthStateListener: () => { dispatch(registerAuthStateListener) }, 
    fetchWishes: () => { dispatch(fetchWishes) }
  }
}

export default connect(
  stateToProps,
  dispatchToProps
)(App);

And I am correctly calling the actions in 's componentDidMount():我正确地调用了 componentDidMount() 中的操作:

  componentDidMount () {
    this.props.registerAuthStateListener()
    this.props.fetchWishes()
  }

Example thunk that is never activated:从未激活的示例 thunk:

import firebase from 'firebase'

export function fetchWishes () {
    console.log("Outside")
    return async (dispatch) => {
        console.log("Inside")
        let querySnapshot = await firebase.firestore().collection('wishes').get()


        let newState = []

        querySnapshot.forEach((wish) => {
            newState.push({
                id: wish.id,
                ...wish.data()
            })
        })

        dispatch({
            type: 'WISHES_FETCHED',
            data: newState
        })
    }
}

Example reducer:示例减速器:

const INITIAL_WISHES_STATE = {
    wishes: []
}

export function wishesStateReducer (state = INITIAL_WISHES_STATE, action) {
    switch (action.type) {
        case 'WISHES_FETCHED':
            return {
                wishes: action.data
            }

        default:
            return state
    }
}
function dispatchToProps (dispatch) {
  return {
    registerAuthStateListener: () => { dispatch(registerAuthStateListener()) }, 
    fetchWishes: () => { dispatch(fetchWishes()) }
  }
}

I wasn't calling the thunk action creators.我不是在打电话给 thunk 动作的创造者。

Everything else in your code looks good but I'm not too sure about this part:您代码中的其他所有内容看起来都不错,但我对这部分不太确定:

return async (dispatch) => {

Try changing that to:尝试将其更改为:

return (dispatch) => {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM