reactjs - 在 ReactJS 中验证来自后端的数据的正确位置是什么?
问题描述
假设我收到了这个 JSON:
"events": [
{
"description": "Some event",
"details": "Issue found",
"id": 0,
"severity": "critical",
"type": "blabla"
},
]
我有一个Component
使用该severity
字段来定义它的 CSS 类(类似于className={e.serveity}
);
如果我收到一个severity
不在预期中的内容(例如critical、warning等),我想执行一些操作,例如渲染另一个组件或完全执行其他操作。
放置此验证码的正确位置是什么?应该是:
- 在
Component
自身内部; - 在
action
承诺的负责人范围内; - 内
reducer
;
解决方案
您应该在组件和组件内执行此操作,您可以在 react 提供的生命周期方法(如 componentWillMount、componentDidMount、componentWillReceiveProps 和渲染)中执行此操作。
请注意,最新的 React v16.3 已弃用 componentWillMount 和 componentWillReceiveProps。
所以你可以根据需要这样做
如果你想在渲染中做
render(){
this.state.events.map(item => {
if(item != “critical” && item != “warning”){
//do stuff here
}
});
return(
);
}
有很多方法可以在组件中执行此类逻辑。所以对于你的问题,推荐的地方是组件。
Actions 用于调度 action,reducer 用于将数据设置为 Redux 状态。
推荐阅读
- d3.js - d3折线图x,y不在同一个数据结构中,如何设置x,y函数?
- python - uri驱动程序中的前缀名称是什么
- json - 如何在Go中的嵌套对象数组中获取最深的对象
- swift - 如何在 Swift 中使用 tokbox sdk 手动停止屏幕共享
- javascript - 如何根据传入的数据类型动态更改表格内图标的呈现
- python - Pandas Dataframe 问题:Groupby 显示最大值
- python - PyGears 将整数转换为定点
- python - 获取两个数据框之间最接近的元素 - 多个条件
- php - Laravel SQL 查询 whereIn + where + orWhere
- excel - 如果使用 VBA Excel 已有值,如何将数据从一张表粘贴到另一张表并添加新行?