react-native - mapStateToProps 不更新组件
问题描述
我是一名经验丰富的 React Native 编码器,为此苦苦挣扎了 4 个小时,感觉就像在浪费生命。mapstatetoprops 不会更新组件。
代码:在组件中:
class UnreadChat extends Component {
...
render() {
console.log('---reloaded', this.props.unreadChatIds);//here it does not get reloaded after mapStateToProps receives
this.newMapStateToProps();
return (
<View>
<Text>
aa
</Text>
</View>
);
}
}
const mapStateToProps = (state) => {
const { unreadChatIds } = state.unreadchats;
console.log('---unreadChatIds', unreadChatIds);
return { unreadChatIds };
};
export default connect(
mapStateToProps, null
)(UnreadChat);
i
n 还原:
const INITIAL_STATE = {
unreadChatIds: []
};
export default (state = JSON.parse(JSON.stringify(INITIAL_STATE)), action) => {
switch (action.type) {
case CHAT_SET:
let unreadChatIds = state.unreadChatIds;
if (action.payload.value === true) {
if (unreadChatIds.includes(action.payload.chatId) === false) {
unreadChatIds.push(action.payload.chatId)
}
}
return { ...state, unreadChatIds };
default:
return state;
}
};
Environment:
OS: macOS High Sierra 10.13.6
Node: 8.10.0
Yarn: 1.5.1
npm: 5.6.0
Watchman: 4.9.0
Xcode: Xcode 9.4.1 Build version 9F2000
Packages: (wanted => installed)
react: 16.2.0 => 16.2.0
react-native: 0.54.4 => 0.54.4
"react-redux": "^5.0.7",
"redux": "^3.7.2"
你能帮我解决一下吗?mapstatetoprops 不再被使用了吗?当 redux 状态发生变化时,有什么方法可以更新组件?
谢谢
解决方案
推荐阅读
- c - 如何将锯齿状数组与C中的另一个数组进行比较?
- ruby-on-rails - 为 Active Admin 和 devise_token_auth 扩展 Rails 应用程序控制器
- r - 来自不同数据文件的相同字符串在 R 中不匹配
- javascript - 如何在反应组件的浏览器范围内存储一些值?
- python - Python 通过 Cron 运行时无法访问环境变量
- r - 使用`open`功能时如何解决URL连接问题
- node.js - TypeORM 中的内连接查询
- android - 无法使用带有 Android Studio 4.0.1 的 kotlin mulitplatform 生成 swift sdk
- cassandra - 如何使用cassandra将指定时间内的数据导出到csv?
- angular - 如何创建一个在打字稿上输出数字的函数?