英文:
How to assign specific modal to buttons in a Redux modal component?
问题
我已经使用Redux和Redux Toolkit构建了一个模态组件。以下是该组件的代码示例:
初始状态:
const initialState = { isOpen: false };
export default initialState;
切片:
const modalSlice = createSlice({
name: "modal",
initialState,
reducers: {
setIsOpen: (state, action) => {
state.isOpen = action.payload;
}
}
})
export const { setIsOpen } = modalSlice.actions;
export default modalSlice.reducer;
选择器:
const modal = (state: RootState) => state.modal;
export const modalSelector = createSelector(
modal,
(state) => state.isOpen
);
模态组件本身非常简单,只是一个具有背景和子元素作为属性的固定div
。
我需要将相同的模态组件用于多种不同内容的情况,但我不确定如何将每个按钮与正确的模态关联起来。当我点击按钮时,它总是打开与该按钮关联的第一个模态,而不是与该按钮关联的模态。如何确保每个按钮打开预期的模态?
英文:
I have built a modal component with Redux and Redux toolkit. This is how the component looks
initialState:
const initialsState = { isOpen: false };
export default initialState;
slice:
const modalSlice = createSlice({
name: "modal",
initialState,
reducers: {
setIsOpen: (state, action) => {
state.isOpen = action.payload;
}
}
})
export const { setIsOpen } = modalSlice.actions;
export default modalSliuce.reducer;
selector:
const modal = (state: RootState) => state.modal;
export const modalSelector = createSelector(
modal,
(state) => state.isOpen
);
The modal component itself is quite simple. is just a fixed div with a background and children as props.
I need to use the same modal component for multiple cases with different content, but I'm unsure how to associate each button with the correct modal. When I click on a button, it always opens the first modal instead of the one associated with that button. How can I ensure that each button opens the intended modal?
答案1
得分: 2
代码部分的中文翻译如下:
代替所有模态框引用的单个布尔状态值,您应该为任何正在切换的模态框引用存储离散状态。
示例:
模态框切片
export const initialsState = {
isOpen: {}
};
const modalSlice = createSlice({
name: "modal",
initialState,
reducers: {
setIsOpen: (state, action) => {
state.isOpen[action.payload.id] = action.payload.isOpen;
}
}
})
export const { setIsOpen } = modalSlice.actions;
export default modalSlice.reducer;
选择器
更新/修改选择器以接受额外的参数,在这种情况下是模态框的 ID 值。
const modalSelector = (state: RootState) => state.modal;
const isOpenSelector = createSelector(
[modalSelector],
(modal) => modal.isOpen,
);
export const modalIsOpenSelector = createSelector(
[isOpenSelector, (state, id) => id],
(isOpen, id) => isOpen[id]
);
要切换特定模态框的 isOpen
值,请使用 setIsOpen
动作并传递模态框的 ID 和打开状态。
dispatch(setIsOpen({
id: "modalA",
isOpen: true
}));
dispatch(setIsOpen({
id: "modalA",
isOpen: false
}));
要打开特定模态框,请使用更新/修改后的 modalIsOpenSelector
函数,并传递模态框的 ID 参数。
const isOpen = useSelector(state => modalIsOpenSelector(state, "modalA"));
英文:
Instead of a single boolean state value that all modals are referencing you should store discrete state for any modal reference that is being toggled.
Example:
Modal slice
export const initialsState = {
isOpen: {}
};
const modalSlice = createSlice({
name: "modal",
initialState,
reducers: {
setIsOpen: (state, action) => {
state.isOpen[action.payload.id] = action.payload.isOpen;
}
}
})
export const { setIsOpen } = modalSlice.actions;
export default modalSlice.reducer;
Selectors
Update/modify the selector to take an extra parameter, a modal id value in this case.
const modalSelector = (state: RootState) => state.modal;
const isOpenSelector = createSelector(
[modalSelector],
(modal) => modal.isOpen,
);
export const modalIsOpenSelector = createSelector(
[isOpenSelector, (state, id) => id],
(isOpen, id) => isOpen[id]
);
To toggle a specific modal's isOpen
value, dispatch the setIsOpen
action with a modal id and open status.
dispatch(setIsOpen({
id: "modalA",
isOpen: true
}));
dispatch(setIsOpen({
id: "modalA",
isOpen: false
}));
To open a specific modal, use the updated/modified modalIsOpenSelector
function and pass in the modal id parameter.
const isOpen = useSelector(state => modalIsOpenSelector(state, "modalA"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论