【发布时间】:2018-10-08 11:43:21
【问题描述】:
Redux 是 harrrrd... 至少对我来说,它是!!!有人可以向我解释如何通过 mapDispatchToProps 将获取的 json[0] 传递给我的动作创建者吗?我做得对吗?我正在使用 redux-thunk,这是正确的使用方式吗?
state = {
articles: {
article: []
}
};
qrCodeOnReadHandler = ({ data }) => {
this.props.onQRRead();
console.log(this.props.art);
fetch(data)
.then(response => response.json())
.then(json => {
console.log(json),
// () => this.props.onQRRead(json[0]),
this.setState({
...this.state,
articles: {
...this.state.articles,
article: json[0]
}
});
});
};
连接redux
const mapStateToProps = state => {
return {
art: state.articles.article
};
};
const mapDispatchToProps = dispatch => {
return {
onQRRead: () => dispatch(article())
};
};
动作创建者
export const fetchedArticle = res => {
return {
type: ARTICLE,
res: res
};
};
export const article = res => {
return dispatch => {
dispatch(fetchedArticle(res));
};
};
【问题讨论】:
标签: reactjs react-native redux redux-thunk