Sitemap
Jack the Nomad

Digital nomad and a software engineer born in 1993, Poland, sharing thoughts and lessons from his travels, work, and connecting with people.

Follow publication

How to write good, composable and pure components in Angular 2+

14 min readJun 7, 2018

--

Solution

1. Divide components into Smart and Dumb

Dumb Component
Smart Component
Zoom image will be displayed
Smart/Dumb x Stateful/Stateless matrix

2. Keep components as Dumb as possible

3. Decide when a component should be Smart instead of Dumb

3a. If it can be Dumb, make it Dumb

3b. If multiple children are equally Smart, make them Dumb

3c. What cannot be Dumb, make it Smart

Zoom image will be displayed
Search page example

3d. If the Smart one gets too big, divide it into separate Smarts.

Frequently Asked Questions

1. How and when am I allowed to inject external services/dependencies? Can I depend on them, and how do I communicate with them?

2. How do I apply a change to my parent? Basically, how do I handle my app to change anything?

3. Can I ever mutate my inputs?

4. I need to get the current city weather in my CityListItemComponent. Can I just inject my tiny WeatherService in it and use it directly like this.cityWeather = WeatherService.getWeatherForCity(this.city)?

5. I need to refresh the current weather for a given city with a refresh button located in my CityListItemComponent. Can I just inject my tiny WeatherService there and call WeatherService.refreshWeatherForCity(this.city) directly?

Pros & cons of following the Smart/Dumb split

Pros

Cons

TL;DR:

--

--

Jack the Nomad
Jack the Nomad

Published in Jack the Nomad

Digital nomad and a software engineer born in 1993, Poland, sharing thoughts and lessons from his travels, work, and connecting with people.

Jack Tomaszewski
Jack Tomaszewski

Written by Jack Tomaszewski

Full-stack web developer (jtom.me) with over 15 years of experience. Past digital nomad and traveller. Now settled for good in Poznań, Poland.

Responses (19)