import { createSlice } from '@reduxjs/toolkit'; import type { RapidAttributesActions,…
- User
import { createSlice } from '@reduxjs/toolkit';
import type { RapidAttributesActions, RapidAttributesSelectors, RapidAttributesState, } from './types';
const INITIAL_STATE: RapidAttributesState = { disabled: {}, label: {}, readonly: {}, visibility: {}, hints: {}, };
export const rapidAttributesSlice = createSlice< RapidAttributesState, RapidAttributesActions, 'rapidAttributesState', RapidAttributesSelectors
({ name: 'rapidAttributesState', initialState: INITIAL_STATE, reducers: { setDisabled: (state, { payload: { attributeCode, value } }) => { state.disabled[attributeCode] = value; },
setAllDisabled: (state, { payload }) => { state.disabled = payload; }, setLabel: (state, { payload: { attributeCode, value } }) => { state.label[attributeCode] = value; }, setAllLabel: (state, { payload }) => { state.label = payload; }, setReadonly: (state, { payload: { attributeCode, value } }) => { state.readonly[attributeCode] = value; }, setAllReadonly: (state, { payload }) => { state.readonly = payload; }, setVisibility: (state, { payload: { attributeCode, value } }) => { state.visibility[attributeCode] = value; }, setAllVisibility: (state, { payload }) => { state.visibility = payload; }, setHints: (state, { payload: { attributeCode, hints } }) => { state.hints[attributeCode] = hints; }, setHintByType: (state, { payload: { attributeCode, hint, hintType } }) => { const attributeHints = { ...state.hints[attributeCode], [hintType]: hint }; state.hints[attributeCode] = attributeHints; }, }, selectors: { attributeDisabledSelector: (state, attributeCode) => state.disabled[attributeCode], attributeHintsSelector: (state, attributeCode) => state.hints[attributeCode], attributeHintByTypeSelector: (state, { attributeCode, hintType }) => state.hints[attributeCode]?.[hintType], attributeLabelSelector: (state, attributeCode) => state.label[attributeCode], attributeReadonlySelector: (state, attributeCode) => state.readonly[attributeCode], attributeVisibleSelector: (state, attributeCode) => state.visibility[attributeCode], attributeStateSelector: (state, attributeCode) => ({ disabled: attributeDisabledSelector(state, attributeCode), readonly: attributeReadonlySelector(state, attributeCode), visibility: attributeVisibleSelector(state, attributeCode), label: attributeLabelSelector(state, attributeCode), hints: attributeHintsSelector(state, attributeCode), }), },});
export const { setAllDisabled, setAllLabel, setAllReadonly, setAllVisibility, setDisabled, setHintByType, setHints, setLabel, setReadonly, setVisibility, } = rapidAttributesSlice.actions; export const { attributeDisabledSelector } = rapidAttributesSlice.selectors; how in selectors call in one selector other selector
- Assistant
In Redux Toolkit, when you want to call one selector from within another selector, you can simply reference it within the function body of the selector that you’re defining. Here’s how to do this in your existing
rapidAttributesSlicecode.You already have the selectors defined. You can modify the
attributeStateSelectorto use the previously defined selectors like this:attributeStateSelector: (state, attributeCode) => { return { disabled: rapidAttributesSlice.selectors.attributeDisabledSelector(state, attributeCode), readonly: rapidAttributesSlice.selectors.attributeReadonlySelector(state, attributeCode), visibility: rapidAttributesSlice.selectors.attributeVisibleSelector(state, attributeCode), label: rapidAttributesSlice.selectors.attributeLabelSelector(state, attributeCode), hints: rapidAttributesSlice.selectors.attributeHintsSelector(state, attributeCode), }; }, - User
не понял
- Assistant
Давайте я объясню, как вы можете вызывать один селектор из другого в вашем коде на примере
rapidAttributesSlice.Когда вы создаете селекторы в Redux, вы можете использовать их внутри других селекторов для получения более сложных значений. В вашем случае у вас уже есть несколько селекторов, и вам нужно реализовать один селектор (), который будет использовать другие селекторы.