Skip to content
  • Home
  • Services
  • Professionals
  • AI Alley
  • Bit Lighthouse
  • Blog
  • Start Now
  • Home
  • Services
  • Professionals
  • AI Alley
  • Bit Lighthouse
  • Blog
  • Start Now
César Ribeiro
on February 28, 2023

Build Decoupled React Components with Inversion of Control

Imagine you’re implementing an e-commerce checkout, and the product manager has temporally chosen PayPal as payment provider. But it’s planned to change the provider for another one, e.g.: Stripe. Looking for small changes in the future, how would you implement this?Inversion of Control probably will help you in this case.

 

What is Inversion of Control (IoC)?

IoC is a programming principle where you invert the control flow of a program, it’s used to decouple your code. You can read more about here, let’s go code.

You have the checkout.component.tsx that is responsible for creating the checkout flow. It renders the product list, total of payment and the payment form that is provided by PayPal.

The problem here is that your checkout flow knows about PayPal, and that can generate a huge refactor in the future.

 

 

 

To prevent this, you can use ElementType and interfaces.

ProviderProps ensure that any provider will implement the correct methods. ComponentProps forces the checkout flow to receive a provider and mount it, passing the given props.

Now we can create a “dumb” component that knows how to control the screen state, like loading and feedback.

And the responsible for managing the payment with PayPal can be isolated, totally decoupling the code.

Now, you can create a simple container that mount the checkout component and pass the correct props.

You can even create a factory method that returns the correct provider in a scenario you have more than one.

I hope that this article helped you to understand how to use inversion of control with react and how to decouple your code to be more maintainable.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Come with us!

Do you love technology as we do? Subscribe to our newsletter and receive articles about everything surrounding state of art software engineering.

Popular posts

AWS for non-engineers — Networking

October 18, 2024

DeepEval Insights: Enhancing LLM Evaluation Efforts

May 9, 2024

Tactics for Streamlining Development – From Design Docs to API Integration

March 22, 2024

#14 – Bit Lighthouse – Talk with Nicole Vasquez

November 29, 2023

#13 – Bit Lighthouse – Talk with Andrea Iorio

November 24, 2023

Popular tags

react
Copyright © 2023 Bitboundaire Corp | All Rights Reserved
299 Alhambra Cir – Suite 403 – Coral Gables, Florida – 33134 – United States
  • Home
  • Services
  • Professionals
  • AI Alley
  • Bit Lighthouse
  • Blog
  • Start Now
  • Home
  • Services
  • Professionals
  • AI Alley
  • Bit Lighthouse
  • Blog
  • Start Now
  • Home
  • Services
  • Professionals
  • AI Alley
  • Bit Lighthouse
  • Blog
  • Start Now
  • Home
  • Services
  • Professionals
  • AI Alley
  • Bit Lighthouse
  • Blog
  • Start Now
Linkedin Twitter Instagram Facebook