• Developer's Commute
  • Posts
  • Do you know how to make composable Stateless? Why is it important? - Developer's Commute newsletter

Do you know how to make composable Stateless? Why is it important? - Developer's Commute newsletter

Today we will explore how to make composable functions stateless. Also, we have some interesting tweets, articles, memes

Good Morning, It’s your captain speaking. Welcome aboard the Developer’s Commute. Today we will explore how to make composable functions stateless. Also, we have some interesting tweets, articles, memes and in the end, Coding Motivation to keep you moving We hope that you enjoy this ride with us and you have a pleasant journey ahead.

Code Philosophy 🌜

How to make composable Stateless? Why is it important?

One important aspect of Android development is making the screens reusable, maintainable and testable.

While we talk a lot about app architecture we do not talk much about cleaner UI. So how do we make the UI cleaner?

The answer is simple, by making them stateless. Let's understand the basics of Stateful and Stateless.

Stateful means that the UI holds the state whereas stateless does not hold any state.

What do you mean by State and how do we hold it?

Let’s make a simple outlined text field in Jetpack compose

In this code, we can observe that the value of the field won’t change while changing the name field until an important keyword is introduced in the code.

“remember” is the keyword which helps us to save the state in the UI. The values survive the recomposition with the help of the remember keyword. It also updates the value as the recomposition occurs. But, it introduces a problem of UI holding the state values.

Why is it a problem for UI to hold state values?

If UI holds a state value then they become less reusable and testable. Think of every composable function you make like a Jetpack Library function. The changes should not occur inside of composable instead should be introduced by the developer.

Let’s take an example:

In this example, we can see the State of the name inside this function. It makes it less reusable and testable. Suppose we are taking first name and last name. We can use Hello Content in both of these cases but we would have to write the function two times.

But if we were to make it stateless like this. Then we only need to write the name variable twice and use the same function.

This is was one benefit of making composables stateless.

There are many benefits to using Stateless Composables:

→ Single Source of Truth

→ Encapsulated

→ Shareable

→ Interceptable

→ Decoupled

We will further explore the remember keyword and the benefits of Stateless composables in an upcoming newsletter. Thank you for reading.

This is for today I hope enjoyed this newsletter. If you enjoyed it please let me know and if I have written something wrong please forgive me.

Thank you for reading

Regards,

Developer Commute’s Captain

Free Ebook 🌠

Developer Meme 🎄

Tweets That You Might Love 🐿️

Tweets That You Will Find Interesting



Here is an interesting tweet for you →

Then you might find this tweet interesting→

Here is something interesting for you→

ArticlesThat You Might Love 🐻

Articles That You Will Find Interesting

Here is an interesting article that you might want to check out →

Here is an interesting article for you →

Developer Motivation 🦔

Goodbye Fellow Developer 🐧

Over and Out Passenger. I hope you enjoyed this journey

I hope you enjoyed this Developer’s Commute. Thank you for joining me.

Your Captain- Hitesh Kohli