【发布时间】:2021-08-18 00:56:47
【问题描述】:
在更新配置文件值的先前场景中,我创建了一个新的存储切片 (updatedProfileDetails) 并在那里存储了一个新对象,但我认为这不是解决我的问题的最佳实践(因为我现在有两个切片 profileDetails & updateProfileDetails),
现在我在 redux 文档中发现,我们可以制作不可变的更新减速器来更改同一切片中的数据而无需改变状态,但在这种情况下(将未见会话设置为 false),API 不会发送对象作为响应,而是只是一条成功消息,
所以,我正在尝试将我的对话 ID 从 Action 传递给 reducer 以检查它并更改此对话的单个值[id]
但我无法正确完成
我的代码: 1- 操作:操作成功后我应该发送 id 的位置
export const updateSeenConversation = (id) => async (dispatch, getState) => {
try {
dispatch({
type: SEEN_CONVERSATIONS_REQUEST,
})
const {
userLogin: { userInfo },
} = getState()
const config = {
headers: {
// headers
},
}
const { data } = await axios.put(
`${process.env.REACT_APP_API_KEY}/conversation/unseen/${id}`,
"",
config
)
// if success data : {success:true , message:"..."}
dispatch({
type: SEEN_CONVERSATIONS_SUCCESS,
payload: id, // usually i send data sent from API as dispatch payload,, to check for the exact conversation to update i tried to send id
})
} catch (error) { //error...})
}
}
2- 我的减速机:
export const conversationsListReducer = (
state = { conversations: {} },
action
) => {
// get conversations works Fine and return a slice conversations{..} that i need to update
switch (action.type) {
case GET_CONVERSATIONS_REQUEST:
return { ...state, loading: true }
case GET_CONVERSATIONS_SUCCESS:
return { loading: false, conversations: action.payload }
case GET_CONVERSATIONS_FAIL:
return { loading: false, error: action.payload }
// here ERROR
// i cant access the exact value i want "unseen" **NB:CHECK store in IMAGE BELLOW**
case SEEN_CONVERSATIONS_REQUEST:
return { ...state, loading: true }
case SEEN_CONVERSATIONS_SUCCESS:
return {state.conversations.conversation.map((conversation) => {
if (conversation._id !== action.payload) {
return conversation // return without change if !== myid
}
return {
...conversation, // make a copy
unseen: false, // change this value to false/true
}
})}
case SEEN_CONVERSATIONS_FAIL:
return { loading: false, error: action.payload }
case USER_LOGOUT:
return { conversations: {} }
default:
return state
}
}
谢谢。
【问题讨论】:
-
您有什么问题或有什么问题(如果有的话)?
-
@DrewReese 就像我说的那样,我的第二个代码(reducer)不起作用我无法对这部分存储使用 redux 不可变更新,我正在尝试更新对话的价值.conversation["id sent from action"]['unseen']
unseen: false来自我创建切片对话的同一个减速器
标签: reactjs redux react-redux axios