React Clean Architecture - kureharyosuke/Microservices-FrontEnd GitHub Wiki
Sample code of React with Clean architecture
μ΄ νλ‘μ νΈλ ν¬κ²λ μΉ μλΉμ€μ ν΄λ¦° μν€ν μ²λ₯Ό λμ νλ, κ·Έλ¦¬κ³ μκ²λ 리λμ€ μν€ν μ²μ ν΄λ¦° μν€ν μ²λ₯Ό ν¨κ» μ¬μ©νκΈ° μν, νλμ μμ μμ΄λμ΄μ μν μ½λμ λλ€.
λΆμ‘±ν λΆλΆμ΄λ κ°μ μ¬νμ Issue λλ Pull Request λ¨κ²¨μ£Όμλ©΄ ν¨κ» λ°μνλλ‘ νκ² μ΅λλ€. βΊοΈ
Language
Use Stack
Typescript, Webpack, React, React-Native, Recoil, Styled-Components
(Recoil > Redux)
https://github.com/falsy/react-with-clean-architecture/tree/v1.8.1
Clean Architecture
λ€μν μν€ν μ²λ€μ΄ κ·Έλ¬νλ― ν΄λ¦° μν€ν μ²κ° κ°λ κΈ°λ³Έμ λͺ©μ μμ κ΄μ¬μ¬λ₯Ό λΆλ¦¬νλ κ²μ λλ€. κ°κ°μ κ΄μ¬μ¬μ λ°λΌ κ³μΈ΅μ λλκ³ , μΈλΆ ꡬνμ΄ μλ λλ©μΈ μ€μ¬μΌλ‘ μ€κ³νλ©°, λ΄λΆ μμμ΄ νλ μμν¬λ λ°μ΄ν°λ² μ΄μ€ UI λ±μ μΈλΆ μμμ μμ‘΄νμ§ μλλ‘ ν©λλ€.
- μΈλΆ ꡬν μμκ³Ό λλ©μΈ μμμ ꡬλΆν©λλ€.
- μν€ν μ²λ νλ μμν¬μ μμ‘΄νμ§ μμ΅λλ€.
- μΈλΆ μμμ λ΄λΆ μμμ μμ‘΄ν μ μμ§λ§, λ΄λΆ μμμ μΈλΆ μμμ μμ‘΄ν μ μμ΅λλ€.
- κ³ μμ€, μ μμ€ λͺ¨λ λͺ¨λ μΆμνμ μμ‘΄ν©λλ€.
Communitaction Flow
κ°λ¨νκ² λ€μ΄μ΄κ·Έλ¨μΌλ‘ νννλ©΄ μμ κ°μ΅λλ€.
Session
μ¬μ©μ λ‘κ·ΈμΈ ν λ°κΈλ μΈμ¦ ν ν°μ μΉ μ€ν 리μ§μ μ μ₯νμ¬ μ¬μ©ν©λλ€. μΉ μ€ν 리μ§λ μ μμμ μ κ·Όν μ μμ§λ§, μν μ½λλ μ νλ¦λλ‘ μ§ννμ¬ 'Infrastructures'μ 'Storege'μμ μ μ΄ν©λλ€. μ΄λ λ³ν μ μλ μΈλΆ ꡬνμ λΆλΆμ΄λ©°, κ·Έ μν μ λ§κ² μμΉνμ¬ μ μ§λ³΄μμ μ©μνκ² ν©λλ€.
Board
'Infrastructures'μμ http ν΅μ μ ν΅ν΄ κ²μν κΈκ³Ό λκΈμ κ°μ Έμ 'Use Case'μμ Comment Entityλ₯Ό ν¬ν¨ν Board Root Entityλ‘ μΊ‘μννμ¬ 'Presenter'λ‘ μ λ¬νλ©° 'Presenter'λ Entity λ°μ΄ν°λ₯Ό 'Components'λ‘ μ λ¬ν©λλ€.
'Components'μμλ μν κ΄λ¦¬ 맀λμ μ 'Entity' λ°μ΄ν° λλ 'View Model'λ‘ μΊ‘μν ν λ°μ΄ν°λ₯Ό κΈ°μ΅νκ³ , λ°μ΄ν°μ μν λ³νμ λ°λΌ Viewλ₯Ό λ€μ 그립λλ€.
Inversion of Control
'Repository'μ κ²½μ° Adapter λ μ΄μ΄μ ν΄λΉνκΈ° λλ¬Έμ 'Use Case'μμλ 'Repository'μ λν΄μ μμμλ μλ©λλ€. κ·Έλ κΈ° λλ¬Έμ 'Use Case'μμλ Domain λ μ΄μ΄ Repository Interfaceλ₯Ό κ°μ§κ³ ꡬννλ©°, μ΄λ μ΄νμ Dependency Injectionλ₯Ό ν΅ν΄ λμν©λλ€.
Directory Structure
./src
ββ adapters
β ββ infrastructures
β β ββ interfaces
β ββ presenters
β β ββ interfaces
β ββ repositories
ββ domains
β ββ aggregates
β β ββ interfaces
β ββ entities
β β ββ interfaces
β ββ useCases
β β ββ interfaces
β β ββ repository-interfaces
β ββ dto
ββ frameworks
ββ web
β ββ di
β ββ components
β ββ hooks
β ββ vm
ββ mobile(React Native)
ββ di
ββ components
ββ android
ββ ios
ββ hooks
ββ vm
- κΈ°λ³Έ λλ ν 리λ ν΄λ¦° μν€ν
μ²μ λ μ΄μ΄λ₯Ό κΈ°μ€μΌλ‘ ꡬμ±νμμ΅λλ€.
[ frameworks / adapters / domains(useCases / entities) ] - μ»΄ν¬λνΈμ λλ ν 리 ꡬ쑰λ μλΉμ€ λλ ꡬμ±μ κ° μ½μλ νμμΌλ‘ μμ λ‘κ² κ΅¬μ±ν©λλ€.
Screenshots
Alias
Web
tsconfig.json
/src/frameworks/web/tsconfing.json
{
"compilerOptions": {
//...
"baseUrl": ".",
"paths": {
"@adapters/*": ["../../adapters/*"],
"@domains/*": ["../../domains/*"],
"@frameworks/*": ["../../frameworks/*"],
"@di": ["./di/index.ts"]
}
},
}
webpack.config.js
/src/frameworks/web/webpack.config.js
{
//...
resolve: {
extensions: [".tsx", ".ts", ".js"],
alias: {
"@adapters": path.resolve(__dirname, "../../adapters/"),
"@domains": path.resolve(__dirname, "../../domains/"),
"@frameworks": path.resolve(__dirname, "../../frameworks/"),
"@di": path.resolve(__dirname, "./di/index.ts")
}
},
}
Mobile
tsconfig.json
/src/frameworks/mobile/tsconfing.json
{
"compilerOptions": {
//...
"baseUrl": ".",
"paths": {
"@adapters/*": ["../../adapters/*"],
"@domains/*": ["../../domains/*"],
"@frameworks/*": ["../../frameworks/*"],
"@di": ["./di/index.ts"]
}
},
}
metro.config.js
/src/frameworks/mobile/metro.config.js
const path = require('path')
const extraNodeModules = {
'@adapters': path.resolve(__dirname + './../../adapters'),
'@domains': path.resolve(__dirname + './../../domains'),
'@frameworks': path.resolve(__dirname + './../../frameworks'),
}
const watchFolders = [
path.resolve(__dirname + './../../adapters'),
path.resolve(__dirname + './../../domains'),
path.resolve(__dirname + './../../frameworks'),
]
module.exports = {
//...
resolver: {
extraNodeModules: new Proxy(extraNodeModules, {
get: (target, name) =>
name in target ? target[name] : path.join(process.cwd(), `node_modules/${name}`),
}),
},
watchFolders,
}
Run Project
1. Mock Server
Install
# $ cd /mock-server
$ npm install
Start
# $ cd /mock-server
$ npm start
2-1. Web
Install
# $ cd /src/frameworks/web
$ npm install
Start
# $ cd /src/frameworks/web
$ npm start
2-2. Mobile(ios)
Install
# $ cd /src/frameworks/mobile
$ npm install
# cocoapods install
$ gem install cocoapods
# $ cd /src/frameworks/mobile/ios
$ pod install
Start
# $ cd /src/frameworks/mobile
$ npx react-native run-ios
Version
v1.9.0 - ChangeLog
https://github.com/falsy/react-with-clean-architecture/blob/master/readme-ko.md