英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论