给定一个带有id的currentUser和一个由各种用户创建的'events'数据库,我试图获取一个过滤的事件列表,其中只包含当前用户创建的事件,换句话说,就是他们的事件。
我的攻击计划是将状态的一部分设置为“过滤器”,它将根据各种事情进行更新,但是现在我甚至无法使最基本的过滤器工作。
错误总是发生在本段正下方的函数中。我有一个错误,保持“currentUser”未定义,或“author_id”未定义或“无法读取属性author_id或未定义。”
当我使用getState检查时,在商店中定义了currentUser。
filterMyEvents() {
let cid = currentUser.id
let e = this.props.events
let myEventsFilter = (events, id) => {
debugger
(cid === e[id].author_id)
}
myEventsFilter = myEventsFilter.bind(null, this.props.events)
this.props.updateFilter(myEventsFilter);
}
过滤发生在Selector.js
中export const allEventsByFilter = (events, filter) => {
if (filter) {
return Object.keys(events).filter(filter);
} else {
return events;
}
}
用户容器:
const mapStateToProps = (state) => ({
currentUser: state.session.currentUser,
requestEvents: requestEvents,
events: allEventsByFilter(state.events, state.filter)
});
const mapDispatchToProps = (dispatch) => ({
requestEvents: () => { dispatch(requestEvents()) },
updateFilter: (filter) => dispatch(updateFilter(filter))
})
过滤操作:
export const FilterConstants = {
UPDATE_FILTER: "UPDATE_FILTER"
};
export const updateFilter = (filter) => ({
type: FilterConstants.UPDATE_FILTER,
filter
});
过滤减速机
const _defaultFilters = () => {
return true;
}
const FiltersReducer = function(state = null, action){
if (action.type === FilterConstants.UPDATE_FILTER){
// const newFilter = {[action.filter]: action.value};
return action.filter;
} else {
return state;
}
};