Add go back functionality
This commit is contained in:
+12
-1
@@ -4,12 +4,23 @@ const navigationSlice = createSlice({
|
||||
slice: 'navigation',
|
||||
initialState: {
|
||||
currentPage: 'Home',
|
||||
history: [],
|
||||
},
|
||||
reducers: {
|
||||
navigate: (state, action) => {
|
||||
const { history, currentPage } = state
|
||||
return {
|
||||
history: history.concat(currentPage),
|
||||
currentPage: action.payload,
|
||||
}
|
||||
},
|
||||
goBack: (state) => {
|
||||
const { history } = state
|
||||
const lastIndex = history.length - 1
|
||||
return {
|
||||
currentPage: history[lastIndex],
|
||||
history: history.slice(0, lastIndex),
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -17,7 +28,7 @@ const navigationSlice = createSlice({
|
||||
// Extract the action creators object and the reducer
|
||||
const { actions, reducer, selectors } = navigationSlice
|
||||
// Extract and export each action creator by name
|
||||
export const { navigate } = actions
|
||||
export const { navigate, goBack } = actions
|
||||
|
||||
export const { getNavigation } = selectors
|
||||
|
||||
|
||||
Reference in New Issue
Block a user