最新消息:20210917 已从crifan.com换到crifan.org

【已解决】React中什么是side effect 副作用

ReactJS crifan 5160浏览 0评论

之前学习React 期间,就看到很多地方提到 side effect 副作用

搞得以为是类似于药物💊的副作用呢

后来看到:

1. MobX 介绍 · MobX 中文文档

大概有点感觉了:

就是额外带的一些效果和作用

但是不是完全理解

所以去找找解释

react what is side effect

gaearon/react-side-effect: Create components whose nested prop changes map to a global side effect

salsita/redux-side-effects: Redux toolset for keeping all the side effects inside your reducers while maintaining their purity.

egoist/side-effect: Create Vue components whose nested prop changes map to a global side effect

3 common approaches to side-effects in Redux apps – Gosha Arinich

“What is a side-effect?

The natural Redux flow is this: some action is dispatched, and as a consequence, some state is changed.

If all your Redux app could do was just to change state as the user played with it, though, would your app be particularly useful? Probably not.

Most apps need to reach out to the outside world to be useful — whether by talking to the server, accessing local storage, recording analytics events, or something else entirely.

That process of calling into the real world is what side-effects are. They are a way of bridging the pure Redux world with the outside world.

Common features of side-effects

Side-effects can happen in response to Redux actions. For example, when a user clicks “Save,” you may want to fire off an AJAX request.

Side-effects may dispatch Redux actions. Like when the save process finishes successfully, you may want to dispatch SAVE_SUCCEEDED; or when it failed, SAVE_FAILED.

They also may not dispatch anything. Some side-effects don’t need to dispatch anything: if you are doing analytics tracking based on Redux actions, you would track things in response to Redux actions, but you will not dispatch anything.”

Managing Side Effects In React + Redux Using Sagas — Jack Hsu

“React+Flux more complicated processes involving coordination of action creators and side-effects are hard to handle. ”

redux-saga/redux-saga: An alternative side effect model for Redux apps

“side effects (i.e. asynchronous things like data fetching and impure things like accessing the browser cache) ”

React Side Effects in Detail – Drugs.com

【总结】

side effect副作用是:和真实的世界进行交互的方式,就叫做 side effect。

比如:

用户点击了 保存 按钮,然后会异步地调用服务器的接口去保存数据。

(对于redux来说,当保存的过程完成后,根据保存结果而分发SAVE_SUCCEEDED或SAVE_FAILED事件)

转载请注明:在路上 » 【已解决】React中什么是side effect 副作用

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

    97 queries in 0.190 seconds, using 23.37MB memory