Showcase: React for business applications
Given the typical technical and structural requirements of a business application what are the recommended technology choices and architectural nuggets?
Architecture / Tech
This is my recommended tech stack for a typical enterprise application. Underlying assumptions of most important requirements
- Long service life (often more than 10 years) and thus good maintainability
- Development with several teams and members changing over the years
- Good structuring possibilities for large and complex applications
- Fast development speed
http://blog.embarc.de/spicker/#9
Is architecture just clueless talking and useless Visios? (Well, it can be...)
Architecture is the sum of the important Processes, Structures, Patterns, Architectural Nuggets and Frameworks. Important can mean
- hard to change, but somewhat risky,
- affecting many teams/people or large parts of the software,
- restricting what can be built reasonably,
- having the potential to make the endeavor fail.
Idea of Architectural Nuggets taken from: https://pkruchten.files.wordpress.com/2020/06/kruchten-2020-northrop-award.pdf
Also from Kruchten: The life of a software architect is a long and sometimes
painful succession of suboptimal decisions taken partly in the dark.
Definition of architecture inspired by: https://martinfowler.com/architecture/
Most important Architectural decisions
Framework
Macro-Structure of modules and dependencies
Development in a monorepo (multi-package repositories)
Build your own lib
Micro Frontends
Micro-Structure within a module / Modular Monolith
Structure module into features
Separate components into smart and dumb
- indicate type of component by folder or file name
- if you decide against this sort of category at least are aware of the concept
- Smart Component
- Dumb Components
- Reference (German)
Component Size and structure
- each component should do a minimal task
- thus be kept at minimal size
- anything larger than 100 lines might be a smell
- a file can contain more than one component
- you can have private components only used in the main component
- will show as a component in tooling
- can be tested separately
- a fragment can also be stored in a local variable
- does not show in tooling and can not be tested in isolation
Code Splitting
Routing
State Management
this is all about flow of control and data
Context
- https://reactjs.org/docs/context.html
- Context is designed to share data that can be considered “global” for a tree of React components, such as the current authenticated user, theme, or preferred language.
- use sparingly and only with smart components
Redux
Redux Toolkit (RTK)
MobX
What's next?
MobX vs. Redux (inspired by Nils Hartmann)
- Redux rather functional programming, MobX rather OO
- A lot of freedom with MobX...
- ...but also a lot of "magic
- For large models sometimes difficult to understand what is actually "observed" where and how
- Observer components rather untypical for React (yet)
- Developer tooling not as good as Redux
- Docs, StackOverflow etc. much less comprehensive than Redux
- Both libraries good choice and can be used
- "No one has been fired for chosing Redux, yet".
Testing
Dedicated document
Styling
- styling is a global concern
- often underestimated
- a real architectural decision
- don't mix different approaches
Options for writing styles
- using globally shared CSS
- CSS modules
- SASS
- Styled Components
Tooling
- https://postcss.org/
- checks CSS and adds browser specific prefixes
- can also bundle, minimize, source maps
- styling with TS
Typing
Less important technical recommendations
Component Style (classes vs functional components w/ hooks)
- classes and functional components are pretty similar
- but
- low level
- high level
- Polling
i18n, l10n
Build / Deployment
Component Libraries
Performance
- web vitals
- Profiling Tools Browser
Create React App
- This project was bootstrapped with Create React App, using the Redux and Redux Toolkit template.
- https://create-react-app.dev/
- https://reactjs.org/docs/create-a-new-react-app.html#create-react-app
- Create new projects with CRA
- Leave existing projects as they are and only rethink once you reach a hard boundary
- also applies to webpack by hand
- Do not create a build from scratch unless you have to
- you keep spending a lot of time maintaining
- mostly still is out of data or you make bad choices
- libs often need different builds and do not work well with CRA
- rollup might be more desirable here
- do it to learn a bit more
- might want to start with CRA and eject to see what is going on
Creation
Tooling and Integration
When CRA starts to become limiting
Options
- eject and ajust config by hand
- this will feel worse and worse over time
- build your own react-template or scripts
- https://www.npmjs.com/package/react-app-rewired
- write a post-install-script for npm that adjusts the webpack config in node_modules/react-scripts
- this is only a temporary solution, at the same time open issue and hope for fix