SOLID in React Native - Part 1
The acronym SOLID addresses a set of five rules in software design. With these rules, we can create software with better understanding, flexibility, and maintainability. In this post, we will introduce the first of these principles - the Single Responsibility Principle, applied to mobile development with React Native, which can also be applied to any React application.
Single Responsibility Principle (SRP) - "A class ought to serve a single, clearly defined purpose, reducing the need for frequent changes."
Let's observe the ListRepos component below and analyze what it's doing:
import React, { useEffect, useState } from 'react';
import { View, Text, Alert, ActivityIndicator } from 'react-native';
import axios from 'axios';
type Repo = {
id: string;
language: string;
name: string;
};
export const ListRepos = () => {
const [repos, setRepos] = useState<Array<Repo>>([]);
const [isLoading, setIsLoading] = useState<boolean>(true);
useEffect(() => {
(async () => {
try {
const { data } = await axios.get(
'https://api.github.com/users/davi1985/repos'
);
setRepos(data);
} catch (error) {
Alert.alert('Something wrong!');
} finally {
setIsLoading(false);
}
})();
}, []);
if (isLoading) {
return <ActivityIndicator size={'large'} color={'#4285F4'} />;
}
return (
<View>
{repos.map((item) => (
<View key={item.id}>
<Text>{item.name}</Text>
<Text>{item.language}</Text>
</View>
))}
</View>
);
};
We can observe that the component is:
Thinking about SOLID, specifically the SRP, we can separate the responsibilities. First, let's isolate the data fetching part into a hook:
import { useState, useEffect } from 'react';
import { Alert } from 'react-native';
import axios from 'axios';
type Repo = {
id: string;
language: string;
name: string;
};
export const useGetRepos = () => {
const [repos, setRepos] = useState<Array<Repo>>([]);
const [isLoading, setIsLoading] = useState<boolean>(true);
useEffect(() => {
(async () => {
try {
const { data } = await axios.get(
'https://api.github.com/users/davi1985/repos'
);
setRepos(data);
} catch (error) {
Alert.alert('Something wrong!');
} finally {
setIsLoading(false);
}
})();
}, []);
return { isLoading, repos };
};
Now, our ListRepos component is cleaner and has fewer responsibilities, see:
领英推荐
import React from 'react';
import { View, Text, ActivityIndicator } from 'react-native';
import { useGetRepos } from './useGetRepos';
export const ListRepos = () => {
const { isLoading, repos } = useGetRepos();
if (isLoading) {
return <ActivityIndicator size={'large'} color={'#4285F4'} />;
}
return (
<View>
{repos.map((item) => (
<View key={item.id}>
<Text>{item.name}</Text>
<Text>{item.language}</Text>
</View>
))}
</View>
);
};
We can further improve. If we observe, each item in our list is being rendered directly in our main component. We can separate it and pass via props what is necessary for its rendering. Notice:
import { Text, View } from 'react-native';
type Props = {
id: string;
language: string;
name: string;
};
export const Repo = ({ id, language, name }: Props) => (
<View key={id}>
<Text>{name}</Text>
<Text>{language}</Text>
</View>
);
With our Repo component created, our ListRepos component becomes simpler.
import React from 'react';
import { View, ActivityIndicator } from 'react-native';
import { useGetRepos } from './useGetRepos';
import { Repo } from './Repo';
export const ListRepos = () => {
const { isLoading, repos } = useGetRepos();
if (isLoading) {
return <ActivityIndicator size={'large'} color={'#4285F4'} />;
}
return (
<View>
{repos.map((item) => (
<Repo {...item} />
))}
</View>
);
};
We can continue to improve. I leave you with some challenges to implement and enhance your understanding of the SRP:
By implementing the Single Responsibility Principle (SRP) in our React Native development, we can establish a more cohesive and modular structure.
In the next post, we'll address another principle, further deepening our software design practices.
React Native Developer at Samta.ai
1 年I create a Whatsapp group for React Native developers to get support and help each others to solve bugs, advice, job searching, etc. https://chat.whatsapp.com/KShEMASv1RjIeakQQW4fPS please join group, So we can get your valuable guidance and support
Full-Stack Developer | JavaScript | Python | React Native | Typescript | React | Redux | NodeJs
1 年Sensacional, conteúdo extremamente relevante quando se trata especialmente da manuten??o do código.
Tech Leader | Full Stack Developer | Next Js | React Js | React Native | NestJS | PHP | SHOPIFY
1 年Que conteúdo enriquecedor!
FullStack Developer | React Native | Expo | React | Node | TypeScript | JavaScript
1 年muito bom, mano!
Full Stack Developer Senior
1 年Congratulations David, the article was very educational ??