Add go back functionality

This commit is contained in:
Sofiya Tepikin
2019-11-22 20:28:14 +01:00
parent e66ca3f8db
commit 026a9c17c3
3 changed files with 18 additions and 7 deletions
+12 -1
View File
@@ -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