import { createSlice } from "@reduxjs/toolkit";
export const productSlice = createSlice({
name: 'products',
initialState: [],
reducers: {
addProduct: (state, action) => {
state.push(action.payload)
console.log(state)
},
deleteProduct: (state, action) => {
// const {title} = action.payload;
return state.filter(item => item.title !== action.payload)
},
searchProduct: (state, action) => {
if (!action.payload.trim()) {
return state
}
else {
const res = state.filter(item => item.title.toLowerCase().includes(action.payload.toLowerCase()));
return res
}
}
}
})
export const { addProduct, deleteProduct, searchProduct } = productSlice.actions
export default productSlice.reducer
Here when i search return search result. but after searching all other products that doesnt match the search query getting deleted from the store.
I need to display the search result when searching, and display all products, if i am not searching
New contributor
Prajwal VK is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.