Javascript 数组中的Redux项的ID正在更新
我正在开发一个react native(expo)events应用程序,我遇到了一个问题,我的模拟事件的id被更新,而它们不应该只更新为整数,而id是字符串,我没有这个问题 基本流程是显示事件列表,当用户按下该事件时,他们将进入该事件视图。我通过使用eventInFocus和events*缩减器,并在**eventView容器中找到我需要的事件来实现这一点 当我按下一个特定的事件时,所有的事件id都会被更新,并且很难调试,因为它表明以前的状态已经有了混淆的id,这非常令人困惑,因为在主视图中呈现项目时没有问题(通过数组循环,使用id作为键)。 如果我选择第一个事件,我会得到一个错误,该事件无法找到,如果我选择其他两个事件中的任何一个,事件ID会随机更新 事件缩减器Javascript 数组中的Redux项的ID正在更新,javascript,react-native,redux,expo,Javascript,React Native,Redux,Expo,我正在开发一个react native(expo)events应用程序,我遇到了一个问题,我的模拟事件的id被更新,而它们不应该只更新为整数,而id是字符串,我没有这个问题 基本流程是显示事件列表,当用户按下该事件时,他们将进入该事件视图。我通过使用eventInFocus和events*缩减器,并在**eventView容器中找到我需要的事件来实现这一点 当我按下一个特定的事件时,所有的事件id都会被更新,并且很难调试,因为它表明以前的状态已经有了混淆的id,这非常令人困惑,因为在主视图中呈现
const defaultEvents = [{
id: 0,
name: 'my birthday',
date: new Date(),
repeatAnnually: false,
wallPaperSource: null
},{
id: 1,
name: 'yosemite trip',
date: new Date(),
repeatAnnually: true,
wallPaperSource: null
}, {
id: 2,
name: 'trip to dublin',
date: new Date(),
repeatAnnually: false,
wallPaperSource: null
}];
const event = (state, action) => {
switch (action.type) {
case EVENT_EDITED:
if(state.id !== action.event.id){
return state;
}
return action.event;
default:
return state;
}
}
function events(state = defaultEvents, action){
switch(action.type){
case ADD_EVENT:
return [
...state,
{
id: action.event.id,
name: action.event.name,
date: action.event.date,
repeatAnnually: action.event.repeatAnnually,
wallPaperSource: action.event.wallPaperSource || null
}];
case EVENT_EDITED:
return state.map(e =>
event(e, action)
)
default:
return state;
}
}
function eventInFocus(state = {}, action){
switch(action.type){
case "UPDATE_EVENT_IN_FOCUS":
return Object.assign({}, state, action.event);
case VIEW_EVENT:
return Object.assign({}, action.event);
default:
return state;
}
}
eventInFocus reducer
const defaultEvents = [{
id: 0,
name: 'my birthday',
date: new Date(),
repeatAnnually: false,
wallPaperSource: null
},{
id: 1,
name: 'yosemite trip',
date: new Date(),
repeatAnnually: true,
wallPaperSource: null
}, {
id: 2,
name: 'trip to dublin',
date: new Date(),
repeatAnnually: false,
wallPaperSource: null
}];
const event = (state, action) => {
switch (action.type) {
case EVENT_EDITED:
if(state.id !== action.event.id){
return state;
}
return action.event;
default:
return state;
}
}
function events(state = defaultEvents, action){
switch(action.type){
case ADD_EVENT:
return [
...state,
{
id: action.event.id,
name: action.event.name,
date: action.event.date,
repeatAnnually: action.event.repeatAnnually,
wallPaperSource: action.event.wallPaperSource || null
}];
case EVENT_EDITED:
return state.map(e =>
event(e, action)
)
default:
return state;
}
}
function eventInFocus(state = {}, action){
switch(action.type){
case "UPDATE_EVENT_IN_FOCUS":
return Object.assign({}, state, action.event);
case VIEW_EVENT:
return Object.assign({}, action.event);
default:
return state;
}
}
eventContainer-列表中每个项目周围的容器(主视图)
viewEventContainer-用于单个事件视图
const mapStateToProps = (state, ownProps) => {
const {events, eventInFocus } = state;
viewEvent = find(events, e => e.id = eventInFocus.id);
return {
event: viewEvent,
ownProps
}
}
const mapDispatchToProps = (dispatch, state) => {
return {
onEditEvent: (event) => {
dispatch(editEvent(event));
}
}
}
const EventViewContainer = connect(
mapStateToProps,
mapDispatchToProps
)(EventView);
查看事件操作
export const viewEvent = (event) => {
return {
type: VIEW_EVENT,
event
}
}
关于redux,我有什么遗漏吗?我的减速机是否结构不良?让我困惑的是为什么它只发生在整数而不是字符串上。提前谢谢你
当我按下事件时的状态,正如您所看到的,先前的状态已经有了事件的变异ID
viewEvent = find(events, e => e.id = eventInFocus.id);
^
here
正如您所看到的,state
在每次调用mapstatetops
时都会发生变异