In the spirit of minimising state I always have a desire to mix data types for a given state property. For instance, if I were creating a React page that rendered a list of items that could either be the entire list of items, or a filtered list of items, I might want to have a state property called itemList and another one called filteredItemList.

Now in filteredItemList I might want to differentiate between a filter query returning no results vs the filteredItemList being empty because a filter is not currently 'set'.

I'm thinking I could set filteredItemList to the string "no filter active" when a filter isn't active. I could also set it as an array with 0 elements when a filter is active but returned zero results. And lastly, if a filter is active and does have results, it would be an array with a length greater than 0.

Doing this, I could test for this property's string value, and/or its array length, to determine what and how to render.

I am thinking that one benefit to doing this would be to obviously minimise the amount of state properties.

I am wondering if there are any inevitable drawbacks to doing something like this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.