1
GDC 2021 年 1 月 27 日
弄一个组件用来展示每行数据的状态,比如 Row
把 10 条数据渲染为 10 个 Row,每个 Row 处理自身的 request 和更新自己的状态 |
2
zuiluo 2021 年 1 月 27 日
看了半天才理解...
request 之后请重新读取一次 current state 再进行操作更新就没问题了,而不是读前面的 state |
3
azcvcza 2021 年 1 月 28 日
既然是同时发请求,为什么不用
`` Promise.all(array.map(val=>{ return new Promise(resolve=>{ return axios.post() }).then(res=>{ resolve(res); }) })).then(resAll=>{ // dealWith allResponse }) `` |
7
zuiluo 2021 年 1 月 28 日
@rexchen94 不管异步或不异步,发出多少请求,最终都是单线程在线性处理,每次都读取 current state,不可能出错。建议 review 一下代码是否有问题
|
8
azcvcza 2021 年 1 月 28 日
那你可能需要一份独立的状态更新了,class component 一般是挂在 this.xxx 上,fc 则是用 useRef 。class component 还有 this.setState(state, callback),在 callback 里拿到最新的 state,fc 里没有,碰到这种问题,好像 dan abramov 建议是优化成用 useReducer 来更新状态,可以参考一下官网例子或者 dan 的 blog,https://overreacted.io/zh-hans/a-complete-guide-to-useeffect/ 子标题为‘解耦来自 Actions 的更新’
|