これを行うには、少なくともいくつかの方法があります。
私はおそらくちょうどオブジェクトへの配列から、あなたの状態のキーを拡大する:
{
active: {}
all: []
}
今、あなたはall
ため、既存の減速を再利用し、active
のための動作を追加することができます
const reducer = (state = {}, action) => {
switch (action.type) {
case UPDATE_LIST: // whatever action is currently updating your list
const nextAll = originalListReducer(state.all, action);
return {
active: nextAll[0],
all: nextAll
};
case NEXT_ACTIVE_ITEM:
const {active, all} = state;
const activeIndex = all.indexOf(active);
const nextIndex = (activeIndex + 1) % all.length;
return {
...state,
active: all[nextIndex]
};
default:
return state;
}
};
active
の状態は、上記originalListReducer
と同様に、あなた自身が呼び出す独自の減速機によって管理され、share the all
state by passing it as an additional argumentになることもできます。何かのように:
const reducer = (state = {}, action) => {
switch (action.type) {
case UPDATE_LIST: // whatever action is currently updating your list
const {all, active} = state;
const nextAll = originalListReducer(all, action);
const nextActive = nextActiveReducer(active, action, nextAll)
return {
all: nextAll,
active: nextActive
};
case NEXT_ACTIVE_ITEM:
const {all, active} = state;
return {
...state,
active: nextActiveReducer(active, action, all)
}
default:
return state;
}
};
const nextActiveReducer = (active, action, all) => {
switch (action.type) {
case UPDATE_LIST:
return all[0];
case NEXT_ACTIVE_ITEM:
return (all.indexOf(active) + 1) % all.length;
default:
return active;
}
};
あなたがこれを行うことができます別の方法は、あなたのアクションクリエイターにロジックをプッシュです。
const rootReducer = combineReducer({
all: allReducer,
active: activeReducer
});
const allReducer = (state = [], action) => {
switch (action.type) {
case UPDATE_LIST:
return action.list;
default:
return state;
}
};
const activeReducer = (state = null, action) => {
switch (action.type) {
case UPDATE_ACTIVE:
return action.nextActive;
default:
return state;
}
};
:今、あなたの減速は、このような
active
と
all
の「ダム」アップデータになる
const nextActiveObject =() => {
return (dispatch, getState) => {
const {active, all} = getState();
const activeIndex = all.indexOf(active);
const nextIndex = (activeIndex + 1) % all.length;
const nextActive = all[nextIndex];
dispatch({ type: UPDATE_ACTIVE, active: nextActive });
}
};
:この場合、あなたは、現在の状態を見てredux-thunkを使用して、適切な「次の」アクティブオブジェクトアクションを派遣することができます
あなたが行きたいのはreally up to youです。
リストの最後の項目に到達したら、最初に戻って最初の要素をもう一度返しますか?または、最後に達すると、アプリケーションはデータの表示を停止しますか? –
一般的にレフィックスの仕事をする方法をもっと理解することができるので、いずれにしても面白いですが、ここで物事をシンプルにするために、いったん終わりに達したら、それはそうです! –
idではなく、インデックスを追跡する必要があります。レジューサーを2つのプロパティーを持つオブジェクトを返すように書き直します.1つはこの配列で、もう1つはクリックしたまま更新するselectedIndexです。選択したインデックスは、最初は0か-1です。必要に応じて –