如何从redux迁移到react-query?

huangapple go评论63阅读模式
英文:

How to migrate from redux to react-query?

问题

我开始学习React Query,我弄不明白。
请根据我的代码示例提供帮助

这里有一个操作和一个Reducer:

export const addHighlighting = parent_id => dispatch => {
  dispatch({
    type: types.HANDLE_COMMENT_HIGHLIGHTING,
    payload: {
      parent_id,
      is_highlighted: true
    }
  })
}

case types.HANDLE_COMMENT_HIGHLIGHTING:
      const parentIndex = state.comments.findIndex(
        comment => comment.id == action.payload.parent_id
      )
      return update(state, {
        comments: {
          [parentIndex]: {
            is_highlighted: { $set: action.payload.is_highlighted }
          }
        }
      })

页面上有一个功能,当鼠标悬停时,会提升状态并突出显示父评论:

onMouseEnter={() => addHighlighting(comment.parent)}
onMouseLeave={() => removeHighlighting(comment.parent)}

现在如何在React Query中使用mutation来实现类似功能?

英文:

I started studying react-query, I can't figure it out.
Please help with an example based on my code

There was such an action and a reducer

export const addHighlighting = parent_id => dispatch => {
  dispatch({
    type: types.HANDLE_COMMENT_HIGHLIGHTING,
    payload: {
      parent_id,
      is_highlighted: true
    }
  })
}

case types.HANDLE_COMMENT_HIGHLIGHTING:
      const parentIndex = state.comments.findIndex(
        comment => comment.id == action.payload.parent_id
      )
      return update(state, {
        comments: {
          [parentIndex]: {
            is_highlighted: { $set: action.payload.is_highlighted }
          }
        }
      })

there was functionality on the page that raised the status and, when hovering the mouse, highlighted the parent's comment.

onMouseEnter={() => addHighlighting(comment.parent)}
onMouseLeave={() => removeHighlighting(comment.parent)}

How now to do also on react-query with mutation?

答案1

得分: 2

React Query的工作是从服务器获取数据并进行缓存。

Redux的工作是将应用程序的数据保存在内存中并进行管理。

如果这些高亮信息没有保存在服务器的某个地方,React Query就不适合这项工作。

英文:

The job of React Query is to get data from a server and cache that. That's all.

The job of Redux is to keep data for your application in memory and manage that.

If this highlighting info is not saved somewhere on a server, React Query is the wrong tool for that job.

huangapple
  • 本文由 发表于 2023年5月15日 15:04:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定