前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Redux-DOM not refreshed

Redux-DOM not refreshed

作者头像
szhshp
发布2022-09-21 10:26:41
1180
发布2022-09-21 10:26:41
举报
文章被收录于专栏:szhshp 的第四边境中转站

Why My DOM not refreshed?

We all know that if state changed then DOM will refresh, if we dispatched an action but the DOM not refreshed, definitely you triggered side effect.

Here is a debug checklist:

  1. Did you trigger CORRECT ACTION?
  2. Did you dispatched CORRECT ACTION TYPE?
  3. Do you have some async/await combined with TRY/CATCH and the errors are catched?
  4. Check the redux debug extension to see the diff of that action
  5. Then, possibly you changed the state DIRECTLY in reducer...
    1. Did you used shallow clone instead of deep clone in reducer?
    2. Spread operator will do shallow clone, did you use that?

Example

Common Mistakes when utilizing spread operator

代码语言:javascript
复制
const rv = (state === undefined) ? defaultState : { ...state };
switch (action.type) {
  case LOGIN_ERROR:
    /* 
        Spread Operator only do shallow copy here
    the grand child are still referenced to state

    that means: after spread operator, rv.loginPage.msg === state.loginPage.msg
    */
  /* YOU ARE CHANGING STATE DIRECTLY  */
    rv.loginPage.msg = {
      ...state.loginPage.msg,
      ...action.data,
    }
    return rv;
}

Best Practise

代码语言:javascript
复制
const rv = (state === undefined) ? defaultState : { ...state };
switch (action.type) {
  case LOGIN_ERROR:

    /*
    spread the object for each generation 
    */
    rv.loginPage = {
      ...state.loginPage, /* we spreaded the child */
      msg: {
        ...state.loginPage.msg, /* we spreaded the grandchild */
        ...action.data,
      },
    };
        
    return rv;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-02-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Why My DOM not refreshed?
  • Example
  • Best Practise
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档