Reactjs 如何将异步数据传递给dispatch action,以便我可以在action creator中使用它?

Reactjs 如何将异步数据传递给dispatch action,以便我可以在action creator中使用它?,reactjs,react-native,redux,redux-thunk,Reactjs,React Native,Redux,Redux Thunk,雷杜是哈罗德。。。至少对我来说是这样!!!有人能告诉我如何通过mapDispatchToProps将获取的json[0]传递给我的操作创建者吗?我做得对吗?我正在使用redux thunk,这是正确的使用方法吗 状态={ 文章:{ 第条:[] } }; qrCodeOnReadHandler=({data})=>{ this.props.onQRRead(); console.log(this.props.art); 提取(数据) .then(response=>response.json())

雷杜是哈罗德。。。至少对我来说是这样!!!有人能告诉我如何通过mapDispatchToProps将获取的json[0]传递给我的操作创建者吗?我做得对吗?我正在使用redux thunk,这是正确的使用方法吗

状态={
文章:{
第条:[]
}
};
qrCodeOnReadHandler=({data})=>{
this.props.onQRRead();
console.log(this.props.art);
提取(数据)
.then(response=>response.json())
。然后(json=>{
console.log(json),
//()=>this.props.onQRRead(json[0]),
这是我的国家({
…这个州,
文章:{
…这个州的文章,
文章:json[0]
}
});
});
};

连接冗余

const-mapStateToProps=state=>{
返回{
艺术:国家物品
};
};
const mapDispatchToProps=调度=>{
返回{
onQRRead:()=>dispatch(article())
};
};

动作创造者

export const fetchedArticle=res=>{
返回{
类型:文章,
res:res
};
};
export const article=res=>{
返回调度=>{
发送(获取文章(res));
};
};
如何通过mapDispatchToProps将此获取的json[0]传递给我的 动作创造者

您必须使您的
onQRRead
接收如下参数:

const mapDispatchToProps = dispatch => {
  return {
    onQRRead: payload => dispatch(article(payload))
  };
};
函数参数的名称是任意的

现在,您可以像刚才那样使用它:

this.props.onQRRead(json[0])

嗨,Nguyễn Thanh Tú,但我应该在fetch内部使用onQRRead吗?因为当我像那样使用它时,id不传递参数,实际上它不发送动作。你能建议我在哪里使用这个.props.onQRRead(json[0])吗?请提供更多关于这个附加问题的信息:)我的意思是,请提供完整的组件实现,实际上它可以工作,就像你说的,我在fetch内部使用了这个.props.onQRRead(json[0]),而不是()=>this.props.onQRRead(json[0])。现在它工作得很好。另一个问题。。。我可以发送更多参数(json[1]、json[2]…)吗?如果可以,当我只有action.payload时,如何更新reducer中状态中的每个属性?我可以做一些像action.payload[1],action.payload[2]这样的事情吗?请再问一个问题:)我一定会帮你的