ReactJS hook vs utility function

Should we use utility functions in react or should everything be components and hooks?

I had this scenario:

Utility function for formatting money.

const formatMoney = (value) => value == null ? '' : '$' + value.toFixed(2)

This was used in more than 200 places. Everything was good, until the new requirement came. Now we were supposed to use dynamic currency, not only dollar.

So, the function formatMoney became dependent of the currently logged user in order to display his currency.

Few thigs that I could do:

  • add second parameter to the function and pass it everywhere manually
  • convert the function into hook/component, so it can get what it needs using hooks, since the logged user is kept in global provider.

The second variant was better, since the information that the function needs is a constant, won’t be changed with something else. It will always come from the current user.

But both of those variants requires to manually refactor all the places that this function was used

The idea of a variable/function is to reuse some logic and it was doing it until it became dependent of something that it can’t get itself, since functions can’t call hooks.

I ended up with something like this

const useFormatMoney = (value) => {   // or a component
   const user = useUserContext()
   return value == null ? '' : user.currency + value.toFixed(2)
}

I wasn’t able to do global replace, because now it has a react rules to follow, it should not be called conditionally, while before it could. After refactoring all those 200+ places, it made me rethink:

Every utility function one day can become dependent of something that is not able to get itself. But if I always use components/hooks they can always read things from providers and would not require to manually refactor all places that are used. So,
Is using utility functions a bad practice? Should we always use hooks/components?

New contributor

Oktay Yuzcan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Is using utility functions a bad practice?

No.

Even if you had named your original function useFormatMoney, it wouldn’t have been a hook. It would have looked like a hook, and you might have used it like a hook, but it still would have been an ordinary function.

You could instead write a hook that returns a function

const useFormatMoney = () => {
   const user = useUserContext()
   return (value) => value == null ? '' : user.currency + value.toFixed(2)
}

This can be called unconditionally, and then the resulting function can be called conditionally. It is a much easier change, you can even keep the name formatMoney at the call sites.

N.b. a refactor is a change in code which keeps the observable behaviour the same, which is not the case here, the new requirement needs a change in behaviour.

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật