【发布时间】:2017-08-02 10:48:40
【问题描述】:
我正在尝试在使用 Redux 进行状态管理的基于反应的应用程序中实现无限滚动我正在尝试在页面滚动上调度操作。但无法达到同样的效果。我的代码在这里
// The Reducer Function
const pinterestReducer = function (state=[], action) {
if (action.type === 'SET_CONTENT') {
state = action.payload;
}
return state;
}
const appReducer = combineReducers({
pinterest: pinterestReducer
})
// Create a store by passing in the pinterestReducer
var store = createStore(appReducer);
window.onscroll = function(ev) {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
// you're at the bottom of the page, show all data
store.dispatch({
type: 'SET_CONTENT',
payload: travelJSON
});
}
};
travelJSON 是一个对象数组。最初,我调度了一个将 travelJSON 的前 12 个对象分配给状态的操作。当用户滚动到页面底部时,我必须分配完整的 JSON 下面是我使用此状态的组件:
// Dispatch our first action to change the state
store.dispatch({
type: 'SET_CONTENT',
payload: travelJSON.slice(0,12)
});
render(
<Provider store={store}><Pinterest /></Provider>,
document.getElementById('page-container'));
【问题讨论】:
-
您没有显示任何可以加载更多数据的代码。
travelJSON是什么? -
更新了问题。请看一看。
标签: javascript reactjs npm redux single-page-application