Untitled
unknown
plain_text
8 months ago
1.5 kB
2
Indexable
Never
Yes, you can convert this to use Redux Toolkit’s createReducer function. Here’s how you might do it: JavaScript import { createReducer } from '@reduxjs/toolkit'; const initialState = { difficulty: 'medium', currentCard: { difficulty: 1 }, hardClicks: {}, mostDifficultCardsFromDeck: [], }; const reducer = createReducer(initialState, { easy: (state, action) => { state.currentCard.difficulty = state.currentCard.difficulty < 32 ? state.currentCard.difficulty * 2 : 32; }, medium: (state, action) => { state.currentCard.difficulty = state.currentCard.difficulty; }, hard: (state, action) => { if (!state.hardClicks[state.currentCard.id]) { state.currentCard.difficulty = state.currentCard.difficulty > 1 ? state.currentCard.difficulty / 2 : 1; state.hardClicks[state.currentCard.id] = true; state.mostDifficultCardsFromDeck = shuffle([...state.mostDifficultCardsFromDeck, state.currentCard]); } else { state.currentCard.difficulty = state.currentCard.difficulty; } }, }); Code généré par l'IA. Examinez et utilisez soigneusement. Plus d'informations sur la FAQ. Please note that the shuffle function is not defined in this code snippet. You would need to import or define it yourself. Also, the createReducer function directly mutates the state, which is why we don’t need to return a new state object. This is due to the use of the Immer library under the hood, which allows us to write simpler immutable update logic.
Leave a Comment