Я опытный кодер 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
п редукс:
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 больше не привыкают?
Каким образом можно обновить компонент при изменении состояния избыточности?
Спасибо