Using Open/Closed Concept in hooks

Using Open/Closed Concept in hooks

SOLID is a design pattern commonly used in various programs. Some developers think that the concepts from SOLID can only be applied in OO (object-oriented) programs.

So, this article will show that we can apply SOLID concepts in our React project, specifically SOLID using hooks.


The original concept of open/closed is:

“software entities (classes, modules, functions, etc) should be open for extension, but closed for modification”.

I took the time to understand the real meaning.

So put it in words for React developers, this meaning: Write hooks/components which you never need to touch again, only re-use them in other hooks/components.

Refer to the code below and think about why the open/closed concept is not followed.

 const useUser = ({ userType }) => {
   const [user, setUser] = useState()

    useEffect(() => {
     const userInfo = getUser()
     setUser(userInfo)
   }, [])

    const updateEmail = (newEmail) => {
     if (user && userType === 'admin') {
       updateUser({ ...user, email: newEmail })
     } else {
       console.error('Cannot update email')
     }
   }

    return { user, updateEmail }
 }        

This code won’t get you fired, but we can always improve, right? So, do you understand why it does not follow the concept ??

Imagine you need to insert a new type of user, you would probably add a new “if” statement with the user’s types and their specificities. By doing this, you are modifying the code instead of extending it. So, now that you know about this concept, follow the code that you will create.

const useUser = () => {
 const [user, setUser] = useState()


 useEffect(() => {
   const userInfo = getUser()
   setUser(userInfo)
 }, [])

 return { user }
}

const useAdmin = () => {
 const { user } = useUser()

 const updateEmail = (newEmail) => {
   if (user) {
     updateUser({ ...user, email: newEmail })
   } else {
     console.error('Cannot update email')
   }
 }

 return { user, updateEmail }
}
        
Danilo Pereira

Mobile Engineer | React Native Developer | React | TypeScript | JavaScript | Mobile Developer | Node

6 个月

Great content Otávio Soares! Thanks for sharing

要查看或添加评论,请登录

Otavio Soares的更多文章

  • BEM Methodology: Simplifying CSS

    BEM Methodology: Simplifying CSS

    Have you ever experienced the chaos of a project where modifying the CSS felt like navigating a maze? You try to tweak…

    3 条评论
  • The Prop Drilling Problem

    The Prop Drilling Problem

    React is a great framework for building larger applications using scalable, reusable, and maintainable components…

  • 4 simple ways to start achieving DRY in React

    4 simple ways to start achieving DRY in React

    The DRY definition The DRY principle is a simple concept, stands for "DON'T REPEAT YOURSELF". This concept has aims…

    6 条评论
  • Creating a Simple Toast Component in React with Custom Hooks

    Creating a Simple Toast Component in React with Custom Hooks

    But first, what are Custom Hooks? A Custom Hook is a technique that allows you to encapsulate a component's logic into…

    3 条评论
  • Context Module Pattern

    Context Module Pattern

    Particulamente, gosto de aprender alguns padr?es de desenvolvimento pois na maioria das vezes sempre s?o solu??es que…

  • Porque evitar utilizar muitos re-exporting ?

    Porque evitar utilizar muitos re-exporting ?

    Publica??o incentivada pela eNe Soluc?es no nosso pilar Never Stop Learning. Você já deve ter visto algum arquivo que a…

社区洞察

其他会员也浏览了