数组中的 Redux 项目正在更新它们的 ID
Redux items in an array are having their ids updated
我正在开发一个 react-native (expo) 事件应用程序,我遇到了一个问题,我的模拟事件的 id 被更新,而当它们不应该仅在它们是 integers,当 id 是 strings 时,我没有这个问题。
基本流程是我显示事件列表,当用户按下该事件时,他们将被带到该事件视图。我通过使用 eventInFocus 和 events* reducers 并在 **eventView 容器中找到我需要的事件来做到这一点。
当我按下某个特定事件时,所有事件 ID 都会更新并且很难调试,因为它声明之前的状态已经有混淆的 ID,这非常令人困惑,因为在呈现项目时没有问题在主视图中(遍历数组,使用 id 作为键)。
如果我 select 第一个事件,我会得到一个错误,指出找不到该事件,如果我 select 任何其他 2 个事件,事件 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;
}
}
eventInFocus 减速器
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 - 列表中每个项目周围的容器(主页视图)
const mapStateToProps = (state, ownProps) => {
return {
ownProps
}
}
const mapDispatchToProps = (dispatch, ownProps) => {
return {
onPress: (event)=> {
dispatch(viewEvent(event));
}
}
}
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);
viewEvent 动作
export const viewEvent = (event) => {
return {
type: VIEW_EVENT,
event
}
}
我对 redux 有什么遗漏吗?我的减速器结构不好吗?令我困惑的部分是为什么它只发生在整数而不是字符串上。提前致谢!
当我按下一个事件时的状态,你可以看到之前的状态已经有了事件的变异 ID
这是您的代码中存在错误的一行:
viewEvent = find(events, e => e.id = eventInFocus.id);
^
here
如您所见,state
在每次调用 mapStateToProps
时都会发生变化。
我正在开发一个 react-native (expo) 事件应用程序,我遇到了一个问题,我的模拟事件的 id 被更新,而当它们不应该仅在它们是 integers,当 id 是 strings 时,我没有这个问题。
基本流程是我显示事件列表,当用户按下该事件时,他们将被带到该事件视图。我通过使用 eventInFocus 和 events* reducers 并在 **eventView 容器中找到我需要的事件来做到这一点。
当我按下某个特定事件时,所有事件 ID 都会更新并且很难调试,因为它声明之前的状态已经有混淆的 ID,这非常令人困惑,因为在呈现项目时没有问题在主视图中(遍历数组,使用 id 作为键)。 如果我 select 第一个事件,我会得到一个错误,指出找不到该事件,如果我 select 任何其他 2 个事件,事件 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;
}
}
eventInFocus 减速器
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 - 列表中每个项目周围的容器(主页视图)
const mapStateToProps = (state, ownProps) => {
return {
ownProps
}
}
const mapDispatchToProps = (dispatch, ownProps) => {
return {
onPress: (event)=> {
dispatch(viewEvent(event));
}
}
}
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);
viewEvent 动作
export const viewEvent = (event) => {
return {
type: VIEW_EVENT,
event
}
}
我对 redux 有什么遗漏吗?我的减速器结构不好吗?令我困惑的部分是为什么它只发生在整数而不是字符串上。提前致谢!
当我按下一个事件时的状态,你可以看到之前的状态已经有了事件的变异 ID
这是您的代码中存在错误的一行:
viewEvent = find(events, e => e.id = eventInFocus.id);
^
here
如您所见,state
在每次调用 mapStateToProps
时都会发生变化。