Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.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
Reactjs 如何从action creator访问redux存储?_Reactjs_Redux - Fatal编程技术网

Reactjs 如何从action creator访问redux存储?

Reactjs 如何从action creator访问redux存储?,reactjs,redux,Reactjs,Redux,我正在尝试将行动联系在一起。在我的当前版本中,当SET_current_USER操作发生时,我希望它修改状态(设置当前用户),然后触发一系列其他副作用任务(获取数据、重建UI等)。我的第一个想法是“好吧,我会在商店里设置一个听众”。。。这导致了前面的问题:我基本上认为设置侦听器是一种反模式 建议的解决方案是“分派”链接在一起的多个操作。我没有遵循如何做到这一点(我的“mapDispatchToProps”基于redux教程,看起来与建议的mapDispatchToProps完全不同),因此我在谷

我正在尝试将行动联系在一起。在我的当前版本中,当SET_current_USER操作发生时,我希望它修改状态(设置当前用户),然后触发一系列其他副作用任务(获取数据、重建UI等)。我的第一个想法是“好吧,我会在商店里设置一个听众”。。。这导致了前面的问题:我基本上认为设置侦听器是一种反模式

建议的解决方案是“分派”链接在一起的多个操作。我没有遵循如何做到这一点(我的“mapDispatchToProps”基于redux教程,看起来与建议的mapDispatchToProps完全不同),因此我在谷歌上搜索了一些关于如何将副作用链接在一起的附加内容,并访问了此页面:

尝试第一个示例,我转到我的action creator文件,该文件如下所示:

(actionCreators.js)

我尝试将“setCurrentUser”操作创建者更改为类似于演示中的内容,但为了简单起见,没有异步部分-只是想看看操作是否触发:

export function setCurrentUser(username) {
    return dispatch => {
        dispatch( { type: SET_CURRENT_USER, payload: username } );
        dispatch( { type: ACTION_TWO, payload: username } );
        dispatch( { type: ACTION_THREE, payload: username } );
    }
}
在我的应用程序中,我的mapDispatchToProps如下所示:

const mapDispatchToProps = {
    setCurrentUser: setCurrentUser,
}
我在switchUser处理程序中调用它,如下所示:
this.props.setCurrentUser(this.state.newUsername)

。。。我得到一个错误,说动作必须是普通对象

我怎样才能把行动联系起来


也许更深层次的问题是,我不知道如何访问store以便能够调用store.dispatch。(这是我前面提到的问题)

我认为您的
mapDispatchToProps
函数应该是这样的:
const mapDispatchToProps={
setCurrentUser:(数据)=>调度(setCurrentUser(数据)),
}
。关于从动作创建者访问存储,您需要从声明它的位置(index.js)导出它。将其导入actionCreators文件,然后在那里使用。内容如下:
在index.js文件中:
export const store=createStore(…)
和actionCreators文件中:
import{store}从“您的index.js文件的路径”
开始。希望这能解决你的问题

您应该能够像这样访问您的存储(以获取请求为例):


你需要thunk来增强你的动作创造者。出现“必须是普通对象”错误的原因是动作创建者返回的是函数(),而不是对象。Thunk允许您返回动作创建者中的函数,使用它,您编写的代码应该可以工作

import { createStore, combineReducers, applyMiddleware } from "redux"
import thunk from "redux-thunk"

const store = createStore(combineReducers({
     data1: reducer1,
     data2: reducer2
}), {}, applyMiddleware(thunk))

希望这能起作用。

我不能留下评论,所以有一个问题:为什么不设置mapState并将状态作为参数传递给已调度的操作

这里是组件材料

class AComponent extends Component{
     ...
     ... onClick={()=>this.props.anAction(this.props.state)}
}

const mapStateToProps = state => {
    return { state: state.YourReducer }
}

const mapDispatchToProps = dispatch => {
    return { anAction: () => dispatch(actions.doAnyStaffWith(state)) }
}

export default connect(mapStateToProps, mapDispatchToProps)(BeerReviewer)
操作文件:

export function actionThree(state) {
     return { type: ACTION_TYPE, state }
}

这就是你想要的吗?

Redux thunk将是实现这一目标的理想方式,但你仍然可以通过将承诺作为有效负载返回来链接操作。(如果你不想使用thunk)

然后你可以给这三个人打电话,比如:

this.props.setCurrentUser(this.state.newUsername)
 .then(newUsername => this.props.actionTwo(newUsername))
 .then(newUsername => this.props.actionThree(newUsername))

您是否使用thunk作为中间件?我没有使用任何中间件(尽管可能会添加thunk来执行异步),我认为您需要redux thunk来调度多个中间件actions@lowcrawler. 你需要thunk来增强你的动作创造者。出现“必须是普通对象”错误的原因是动作创建者返回的是函数(),而不是对象。Thunk允许您返回动作生成器中的函数,使用它,您编写的代码应该可以工作。因此,演示假定添加了Thunk@yourfavoritedev-将您的评论作为答案,我会将其标记为正确的(假设它有效…我现在将实现它)redux文档似乎建议使用mapDispatchToProps()的对象形式值得注意的是,对于redux devtools,我最终不得不在createStore过程中使用compose
export function actionThree(state) {
     return { type: ACTION_TYPE, state }
}
export function setCurrentUser(username) {
    return { 
      type: SET_CURRENT_USER, 
      payload: new Promise(resolve => resolve(username))  
    }
} 

export function actionTwo(username) {
    return { 
      type: ACTION_TWO, 
      payload: new Promise(resolve => resolve(username))  
    }
}

export function actionThree(username) {
    return { 
      type: ACTION_THREE, 
      payload: new Promise(resolve => resolve(username))  
    }
}
this.props.setCurrentUser(this.state.newUsername)
 .then(newUsername => this.props.actionTwo(newUsername))
 .then(newUsername => this.props.actionThree(newUsername))