Comments (2)
业务逻辑常见错误
使用“data-”属性在 DOM 元素中存储重要的业务数据
数据集属性绝不意味着存储业务数据。它们是 DOM API 的一部分。
而 DOM 一直处于应用程序规模中称为“视图”、“表示”、“UI”的那部分。它与您的应用程序的业务逻辑无关。
它的存在只是为了将这个逻辑的结果呈现给用户并从他们那里收集反馈
将业务逻辑与 UI 耦合 [逻辑取决于 UI]
这严重违反了单一职责原则(SOLID 中的“S”)。
每次更改这些 UI 元素时,都存在业务逻辑发生潜在变化的风险。
example
componentDidMount() {
axios.get(‘/articles/’)
}
methods: {
calculateDiscount(){
// actual code that
// makes business-critical calculations
// for the price of the product
}
}
应用程序的业务逻辑,软件中最有价值和最关键的部分,依赖 第三方解决方案
混合逻辑和存储
这违反了关注点分离原则
Store 只有一个职责:存储数据。它与业务逻辑无关
Store不关心您如何、从何处以及为什么获取数据。它只负责存储并明确地提供给 UI
example
将业务逻辑与
Flux/Redux/Vuex
或任何其他第三方解决方案结合在一起
from blog.
React业务逻辑 React business logic
使用 hooks 隔离业务逻辑
example
UI 部分
// Presentational component
const QuantitySelector = () => {
const { onClickPlus, onClickMinus, state } = useQuantitySelector();
const { message, value } = state;
return (
<div className="quantity-selector">
<button onClick={onClickMinus} className="button">
-
</button>
<div className="number">{value}</div>
<button onClick={onClickPlus} className="button">
+
</button>
<div className="message">{message}</div>
</div>
);
};
export default QuantitySelector;
业务逻辑部分
import { useState } from "react";
// Business logic. Pure, testable, atomic functions
const increase = (prevValue, max) => {
return {
value: prevValue < max ? prevValue + 1 : prevValue,
message: prevValue < max ? "" : "Max!",
};
};
const decrease = (prevValue, min) => {
return {
value: prevValue > min ? prevValue - 1 : prevValue,
message: prevValue > min ? "" : "Min!",
};
};
// Implementation/framework logic. Encapsulating state and effects here
const useQuantitySelector = () => {
const [state, setState] = useState({
value: 0,
message: "",
});
const onClickPlus = () => {
setState(increase(state.value, 10));
};
const onClickMinus = () => {
setState(decrease(state.value, 0));
};
return { onClickPlus, onClickMinus, state };
};
export default useQuantitySelector;
from blog.
Related Issues (20)
- Blender HOT 1
- 更便宜的3D MMO服务器:GPGPU
- C++ HOT 1
- Unreal Engine 虚幻引擎 HOT 9
- MMORPG + Unreal 5 架构 HOT 6
- C/C++ 工程化 HOT 12
- Cake(C# Make) HOT 6
- NUnit HOT 1
- KiCad HOT 3
- DALL-E 3 HOT 1
- 虚幻引擎 表演捕捉 Live Link HOT 1
- Unreal Engine 5 程序化内容生成框架(PCG) HOT 5
- 虚幻引擎 Unreal Engine 5 C++ HOT 14
- 虚幻引擎5 材质系统 Unreal Engine 5 Material HOT 6
- 虚幻引擎5 游戏技能系统(GAS) Unreal Engine 5 Gameplay Ability System HOT 29
- Unreal Engine 虚幻引擎5 Lyra 项目 HOT 5
- 虚幻引擎5 之旅 Unreal Engine 5 Tutorials HOT 17
- Lyra UML HOT 2
- 完整的游戏动画师流程,从Blender到游戏引擎 Complete game animators pipeline from Blender to engine HOT 1
- 精通 NestJS – Node.js 框架 Master NestJS 9 - Node.js Framework HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.