M
M
mDrama2020-05-23 19:04:45
React
mDrama, 2020-05-23 19:04:45

Is it bad if the redux action works "for nothing"?

Good afternoon. I'm trying to implement autocomplete for a search string. And according to the idea, when someone clicks on the result that has dropped out from the autocomplete, there is a transition to the corresponding page (the path in the router). In this case, the value of the input with the search should be reset to zero, as well as the array in the editor, to which the search results come.

I use hooks and couldn't think of anything better than checking location.pathname in useEffect:

const [inputValue, changeValue] = useState('');

useEffect(() => {
    return () => {
      changeValue('');     // обнуляет значение инпута 
      fetchSearch('');      // это экшен. Если придет пустое значение, то он обнулит массив с результатами поиска
    }
}, [location.pathname, fetchSearch]);


The problem is that this action now fires on every transition. It does nothing complicated, it just replaces the existing array in redux with an empty one. I can also check in the editor itself that if the array is already empty, then do nothing at all. But the action will still work when you go to another page. Is it bad, or if nothing "heavy" happens because of it, then it's normal?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question