我是一位经验丰富的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 redux:
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状态更改时,如何更新组件?
谢谢