[React] Directly Dispatch Actions into the Redux Store Before React Renders

This lesson demonstrates that you can get a small performance gain by removing AJAX calls that feed into redux from the react/hooks lifecycle. In this video we see a 100ms reduction in the time it takes to initiate our AJAX call to load AJAX rates when we move it outside of useEffect and into our index.js file.

A thorough look at some of these patterns can be found here: https://redux.js.org/tutorials/essentials/part-5-async-logic

Documentation for all of the methods available on our redux store including dispatch and getState can be found here: https://redux.js.org/api/store/#store-methods-1

Note: If you want to keep the logic inside of the useEffect and still get the performance benefit you kick off your API calls somewhere and store the generated promise somewhere and pick that up in your useEffect hook.

Syntax:

const store = configureStore({ reducer: counterReducer })

const exampleThunkFunction = (dispatch, getState) => {
  const stateBefore = getState()
  dispatch(increment())
}

store.dispatch(exampleThunkFunction)

 

Dispatch async event for initial state can be done outside of component. Can do it in index.js file before rendering anything.

posted @   Zhentiw  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-04-11 [RxJS] Extend Promises by Adding Custom Behavior
2019-04-11 [Algorithm] Binary tree: Level Order Traversal
2019-04-11 [SSH] Intro to SSH command
2018-04-11 [Tailwind] Extending Tailwind with Responsive Custom Utility Classes
2018-04-11 [Tailwind] Control What Variations are Generated for Each Utility Class Module in Tailwind
2016-04-11 [RxJS] Creation operator: of()
点击右上角即可分享
微信分享提示