javascript - 处理多个数据时反应 native-redux 出错
问题描述
我正在使用 react native-redux 来设置全局状态,它在处理单个数据时工作正常,但是当我有多个数据时它不起作用。下面是我的代码:
import {connect} from 'react-redux'
constructor(props){
super(props)
this.state = {
comment:'',
region:[],
}
}
<Button
onPress={() => {
this.setState({
comment : this.state.comment,
// works fine if I only handle comment or region
region : this.state.region,
},
() => this.props.commentHandler(this.state),
() => this.props.regionHandler(this.state)}}>
<Text>UPDATE</Text>
</Button>
function mapStateToProps(state) {
return {
comment : state.comment,
region : state.region
}
}
function mapDispatchToProps(dispatch) {
return {
commentHandler : (state) => dispatch({ type: 'COMMENT', payload: state.comment}),
regionHandler : (state) => dispatch({ type: 'REGION', payload: state.region})
}
}
我不知道我做错了什么,但是当我尝试处理多个数据时,例如,评论和区域它将不起作用。如果我删除 () => this.props.commentHandler(this.state)
或() => this.props.regionHandler(this.state)
不一起工作,它会完美工作。
知道我可能做错了什么吗?任何意见或建议将不胜感激!
解决方案
将我的处理程序包装到一个函数中使其工作。感谢@Paulquape。
<Button
onPress={() => {
this.setState({
comment : this.state.comment,
region : this.state.region,
},
() => this.props.regionHandler(this.state),this.props.commentHandler(this.state),
() => console.log(this.props.comment,'this.props.comment?'),
() => console.log(this.props.region,'this.props.region?'))
}}>
推荐阅读
- java - 使用 Socket IO 重新连接到旧连接
- python - 从 Dataframe 在 Python Docx 中添加表非常慢
- oracle - 动态执行 RAISE 错误时出错
- python - Gedit python插件滚动1行(Ubuntu 20.04)
- c# - 尝试插入具有 1:N 关系的实体时,重复键值违反了 EntityFramework 中的唯一约束“PK_Users”错误
- python - 在语料库中查找包含两个“g”(不一定相邻)的单词
- javascript - 使用在 showFileUpload 之后发送表单的自定义函数进行反应和上传
- android - Android Studio Preview 看起来很模糊
- julia - 如何在 Flux.jl 中进行主动学习?
- typescript - 指定对象中键的类型