简体   繁体   English

在react-redux中调度动作

[英]Dispatching an action in react-redux

I am trying to dispatch an action using react-redux. 我正在尝试使用react-redux调度一个动作。 I have watched several tutorials and read the documentation, however, I am still not sure what I am doing wrong. 我看了几本教程并阅读了文档,但是,我仍然不确定自己在做什么错。

Here is some code: 这是一些代码:

Container.js Container.js

import React from 'react';
import {connect} from 'react-redux';
import * as Actions from '../../../actions'
import { bindActionCreators } from 'redux'

class Searchbar extends React.Component {
   // some constructor here
   // some methods
   onClickAction(){
      let {rememberUserQuery} = this.props.actions
      console.log(this.props.userQuery) //empty as expected
      rememberUserQuery(someInputDeclaredInsideTheMethod)
      console.log(this.props.userQuery) //still empty instead of being updated
   };
   // some rendor
  }
}

function mapStateToProps(store) {
  let {userQuery} = store.landingPgReducer;
  return {userQuery}
}

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

export default connect(mapStateToProps, mapDispatchToProps)(Searchbar)

Reducers.js Reducers.js

import {
  REMEMBER_USER_QUERY
} from '../actions'

// Reducer to keep track of which form should be shown
export function landingPgReducer(state = {userQuery:''}, action) {
  switch (action.type) {
    case REMEMBER_USER_QUERY:
      console.log(state) //the reducer gets called and has blank state as expected
      console.log(action) //the reducer is called with inputs as expected
      return Object.assign({}, state,
        action.userQuery
      )
    default:
      return state
  }
}

Actions.js Actions.js

export const REMEMBER_USER_QUERY = 'REMEMBER_USER_QUERY'

export function rememberUserQuery(userQuery) {
  console.log('rememberUserQuery',userQuery) //never getting to that console.log
  return {
    type: REMEMBER_USER_QUERY,
    userQuery 
  }
}

PS I have combined reducers in another file and created a store to which I am connecting. PS:我在另一个文件中合并了reducer,并创建了一个我要连接的商店。

UPDATED: I am trying to change global state so that I render accordingly in another component to which I navigate using this.props.history.push("/reports") right after I call the reducer with the action. 更新:我正在尝试更改全局状态,以便在我调用带有操作的reducer之后,使用this.props.history.push(“ / reports”)在要导航到的另一个组件中进行相应的渲染。 In this other component the global state has not changed and the value of 'userQuery' is still '', as the console.logs up in the code are showing. 在此其他组件中,全局状态未更改,并且'userQuery'的值仍为”,如代码中显示的console.logs所示。

Try amending the object you pass to Object.assign() in the return statement of your reducer to the following: 尝试将简化器的return语句中传递给Object.assign()的对象修改为以下内容:

Reducers.js Reducers.js

import { REMEMBER_USER_QUERY } from '../actions'

// Reducer to keep track of which form should be shown
export function landingPgReducer(state = { userQuery: '' }, action) {

  switch (action.type) {

    case REMEMBER_USER_QUERY:
      console.log(state) // the reducer gets called and has blank state as expected
      console.log(action) // the reducer is called with inputs as expected

      return Object.assign({}, state, {
        userQuery: action.userQuery
      })

    default:
      return state

  }

}

If action.userQuery just contains a string from a user input then you will just be returning the original state as you are currently just passing a string to Object.assign() when you should be passing an object with the properties that you want to overwrite. 如果action.userQuery仅包含来自用户输入的字符串,那么当您要传递具有要覆盖的属性的对象时,您将仅返回当前状态,因为您当前正在将字符串传递给Object.assign()

Change your reducer like below. 如下更改减速器。 If your 'action.userQuery' return Object use 如果您的“ action.userQuery”返回Object使用

return { ...state, action.userQuery }

else 其他

return {...state, userQuery: action.userQuery }

Reducer 减速器

import {
  REMEMBER_USER_QUERY
} from '../actions'

// Reducer to keep track of which form should be shown
export function landingPgReducer(state = {userQuery:''}, action) {
  switch (action.type) {
    case REMEMBER_USER_QUERY:
      console.log(state) //the reducer gets called and has blank state as expected
      console.log(action) //the reducer is called with inputs as expected
      return {...state,
        userQuery: action.userQuery
      }
    default:
      return state
  }
}

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

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