Presentation Logic #376
Replies: 3 comments 5 replies
-
As of now I am keeping Date, Number and Name Formatters as part of State, this allow me easily use them then constructing ViewState from State. But I am feeling that it is the wrong way to do it and instead they should be part of Environment. I would love to hear more opinions on this topic. |
Beta Was this translation helpful? Give feedback.
-
I keep formatting logic in reducer. If I want to use an external formatter, I have it in the environment. My rule of thumb is that the ViewStore provides the view with the information that are already processed and ready to be shown without additional work. So formatting, sorting, filtering etc. is done in reducer. |
Beta Was this translation helpful? Give feedback.
-
I don't think presentational code belongs in your store (and therefore your state/environment) at all. It's inherently part of your view layer. There's a number of ways of wrapping up this presentation logic:
Adding this presentational logic to your store doesn't give you any real benefits and may actually complicate testing - you would be much better IMO testing this presentational logic by creating instances of your views, passing in a store with pre-configured state, and snapshot testing them. This allows you to test your view renders correctly in all the different states you want to test and potentially across all kinds of presentational configurations - different locales, color schemes, light/dark mode, screen sizes etc. A compromise between the two approaches above, where you want something re-usable but don't want to necessarily have an entire wrapper around your view store, is to extend ViewStore constrained to the state you are trying to present, e.g.: extension ViewStore where State == SomeState {
var someFormattedValue: Any { }
} You could then re-use this formatting logic on any screen that uses a view store for this type of state and you could unit test these small formatting helpers if needed. This extends on the idea of the Re: the above comment, I don't really see sorting and filtering as the same thing as formatting - the latter is generally a purely presentational concern, where as how your data is sorted or filtered is actual application logic and is something you'd want to keep inside your store. I'd also keep any user-controllable formatting inside the store as that feels more like application logic than purely presentational logic. |
Beta Was this translation helpful? Give feedback.
-
Hi all,
I'm wondering where is the place for presentation logic in TCA?
For example, I have to display date and some numbers from my model on the screen. Easiest way is to instantiate some formatters in the view, and then to use them with values from the State objects. But I guess that this is wrong on many levels.
Next, I can have some sort of Presenter that will be responsible for formatting values, and formatters will live inside it. In that case presenter should produce State or be a State object. If it should provide state how it should be instantiated and who will be its owner?
Next, maybe I can provide formatters as environment injection, but in that case it is not clear should I pass somehow formatters to the view and maybe initialize TextField with formatter? I don't see any way to do that. I can use formatter in environment when value is being set to the state in the reducer, but I don't know how to use them when I'm getting value from State object.
Does anybody have strategy for cases like these?
Beta Was this translation helpful? Give feedback.
All reactions