简体   繁体   English

使用dispatch()时未调用reducer

[英]Reducer not being called when using dispatch()

I am trying to update my redux state from a child component( QResults.js ) by calling a function that I pass to it but my reducer isn't being reached when I use the function. 我试图通过调用传递给它的函数从子组件( QResults.js )更新我的redux状态,但使用该函数时未达到我的reducer。 QResults.js has a link that I am clicking which I expect to alter my state via one of my reducers. QResults.js有一个我单击的链接,我希望通过我的一个减速器来更改我的状态。 Am I doing something wrong with my mapDispatchToProps() function? 我的mapDispatchToProps()函数有问题吗?

Channel.js Channel.js

class Channel extends Component {
    ...
    render() {  
        return (
        ...
         <div>
            <QResults
             allQueryResults={this.state.queryState}
             requestHandler={queueNewRequest}/>
         </div>
        );
    }
}

function mapStateToProps(state) {
   ...
}

 function mapDispatchToProps(dispatch) {
    return ({
        queueNewRequest: (newRequestData) => { dispatch(queueNewRequest(newRequestData)) }
    })
} 

export default withRouter(connect(mapStateToProps , mapDispatchToProps )(Channel))

QResults.js QResults.js

export default class QResults extends Component {
    render() {
        const {requestHandler} = this.props

        return (
            <ul>
                {this.props.allQueryResults.items.map((trackAlbum, i) =>
                    <li key={i}>
                        <a href='#' 
                        onClick={
                            () => requestHandler(trackAlbum.name)}>
                            Some link
                        </a>
                    </li>
                )}
            </ul>
        )
    }
}

Reducers.js Reducers.js

import { combineReducers } from 'redux'

function reducer1(state = {}, action) {
    ...
}

function reducer2(state = {}, action) {
    switch (action.type) {
        case QUEUE_NEW_REQUEST:
            return{
                ...state,
                newRequestInfo : action.newRequestInfo
            }
        default:
            return state
    }
}

const rootReducer = combineReducers({
    reducer1,
    reducer2
})

export default rootReducer

Actions.js Actions.js

export const QUEUE_NEW_REQUEST = 'QUEUE_NEW_REQUEST'

export function queueNewRequest(newRequestInfo) {
    return dispatch  => {
        return {
            type: QUEUE_NEW_REQUEST,
            newRequestInfo
        }
    }
}

Your action doesn't dispatch the action to the reducer. 您的操作不会将操作分派给减速器。 You just passed it in as an argument. 您只是将其作为参数传递了。 I also slightly updated the pass of the param to a key called "payload". 我还将参数的传递略微更新为称为“有效载荷”的键。 Try updating it like this 尝试像这样更新

I've created a minimal sandbox here 我在这里创建了一个最小的沙箱

If you click on one of the items and check your console you can see the reducer is being called. 如果单击其中一项并检查控制台,则可以看到正在调用reducer。

export const QUEUE_NEW_REQUEST = "QUEUE_NEW_REQUEST";

export function queueNewRequest(newRequestInfo) {
  return dispatch =>
    dispatch({
      type: QUEUE_NEW_REQUEST,
      payload: newRequestInfo
    });
}

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

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