I am trying to develop a function that filters an array based on a search input. The goal is for the filter action to trigger when there's a change in the SEARCH_TEXT. However, I'm facing confusion when it comes to handling the state when the delete key is pressed and the text field becomes empty. My initial thought was to use the initial state in the else statement, but I have doubts about whether this approach is correct or not. Returning just the state seems ineffective as it has already been manipulated in the if statement.
Here is a simple example:
const initialState = ['hello', 'wahhh', 'yo'];
export default function searchSimple(state = initialState, action) {
switch (action.type) {
case SEARCH_TEXT:
if(action.text.length > 0){
return state.filter(item =>
item.startsWith(action.text)
)
}
else {
return state
}
}