Giter Site home page Giter Site logo

kidmam / react-clean-architecture Goto Github PK

View Code? Open in Web Editor NEW

This project forked from eduardomoroni/react-clean-architecture

0.0 1.0 0.0 50.09 MB

A realistic approach to implement clean architecture on react codebases

TypeScript 61.79% Python 6.66% Java 5.45% JavaScript 1.16% Objective-C 16.96% HTML 6.09% CSS 1.90%

react-clean-architecture's Introduction

React Clean Architecture

Applying clean architecture to a react codebase brings lots of benefits, most of them you can find by simple googling what's clean architecture and what should we adopt architectural patterns. One advantage that strikes me is having business rules isolated from framework specific things. This means that our core logic is not coupled to React, React Native, Express, etc...
This gives you enough flexibility to, for example, move specific parts of the application to a backend, change libraries without too much pain, test once and reuse as many times as you want, share code between React and React native applications, among others.
This is a realistic approach, what I mean by that is: It's simple enough to be applicable and Robust enough to have it in a production environment. Although I have greatly simplified it, for educational purposes, I believe that this example is of great value to get you started with applying architectural patterns and adapting them to your own needs.

Detailed explanation

I've been pretty busy lately, so I'll write as much as possible.
I'll write three blog posts explaining better saying What is Clean Architecture, Why adopt it and how. Portuguese version of How to implement clean architecture for React codebases can be found HERE

Philosophy

high-level-diagram

The nomenclature may vary, but the concept behind this architectural pattern is: the domain dictates how tools should be organized and not the other way around. What I mean by that is that we should organize our codebase around the business rules and not around the frameworks we use to achieve business rules. The diagram above shows how the dependency rule works, the inner circles must not know about the outer circles. That is, there cannot be an import of a use case within an entity, or import of a framework within a use case. Another important rule is: entities and use cases should not rely on external libraries. The explanation is simple, the core of our application must be robust enough and malleable enough to meet the demands of the business without needing any external intervention. If by chance, an essential part of the application core MUST BE an external dependency. Dependency needs to be modeled following dependency inversion principle.

Communication flow

communication-flow-diagram

A brief explanation of each responsibility

  • Entity: Application independent business rules
  • Interactor: Application especific business rules
  • Adapter: Glue code from/to interactors and presenter, most of the time implementing a framework specific behaviour. e. g.: We have to connect interactor with react container, to do so, we have to connect interactor with redux (framework) and then connect redux to container components.
  • Presenter: Maps data from/to adapter to/from components.
  • Components: Simplest possible unit of presentation. Any mapping, conversion, MUST be done by presenter.

Sample apps DEMO

Talk is cheap, don't you think? That's why I'm sharing two sample apps to facilitate your digestion.
A great advantage of following clean architecture is having all business logic self-contained and closer, in a readable way.
Take a look at core/entities/ and core/useCases/ folders and see for yourself.

Counter

Counter app is a simple example of how to apply clean architecture to react world, it uses only synchronous actions and has no external dependencies.
It contains a single business rule:

  • The counter can not be negative and can not be greater than 10.

counter-gif


Authentication

Authentication app is a simple example, but no to simple, of how to apply clean architecture to a realistic scenario. It contains some shared business rules:

  • Users must have a valid email.
  • Users password cannot contains anything but numbers and letters.
  • Users name must have full name, and it has to be lower case.
  • The App cannot sign up two users with same email address.
  • The app must use an external dependency to persist user register.

authentication-gif


Folder Structure

This repository contains 2 examples of how to implement react following clean architecture, both follow same folder structure which represents diagram showed above.

./counter
├── core
│   └── lib
│       ├── adapters
│       │   └── redux
│       ├── entities
│       ├── frameworks
│       └── useCases
├── native
│   └── src
│       ├── components
│       └── stylesheets
└── web
    └── src
        ├── assets
        ├── components
        └── stylesheets

Note: frameworks folder is basically framework setup to have it available to the adapters.

How to Run

run npm install under the project you'd like to run, and then run npm start.

References

Thanks

Feedback

If something looks odd, don't hesitate to reach me out or opening an issue.

react-clean-architecture's People

Contributors

eduardomoroni avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.