正在调度操作以更新Redux状态

neskvpey  于 2022-11-12  发布在  其他
关注(0)|答案(1)|浏览(164)

我有一个张贴的UI,我正在建设,并试图使用redux维护。
我目前有一个切片,类似于:

const jobsearchSlice = createSlice({
  name: "jobsearchSlice",
  initialState: {
    isFetching: false,
    searchComplete: false,
    roles: [],
    error: false,
    finalSelection: false,
    selectedRole: "",
    selectedRole: [],
    selectedStyle: ""
  },
  reducers: {
    searchStart: (state) => {
      state.isFetching = true;
      state.searchComplete = false;
      state.finalSelection = false;
    },
    searchSuccess: (state, action) => {
      state.isFetching = false;
      state.roles = action.payload;
      state.searchComplete = true;
      // state.searchComplete = false;
    },
    searchFailure: (state) => {
      state.isFetching = false;
      state.error = true;
    },
    searchComplete: (state, action, term) => {
      state.isFetching = false;
      state.roles = action.payload;
      state.searchComplete = true;
      state.finalSelection = true;
      state.selectedRole = term;
    },
    searchClear: (state, action) => {
      state.isFetching = false;
    },
    searchUpdateStyle: (state, action, option) => {
      state.selectedStyle = option;
    },
  },
});

除了searchUpdateStyle之外,所有这些函数都可以工作。
我的组件是这样的,我想将选择分派到Redux:

{/* style! */}
                    {
                    page === 0 && 
                    <Dropdown>
                      <Dropdown_Button
                      onClick={e => 
                          setisActive(!isActive)}
                      >
                          {/* {selectedStyle || "Choose style"} */}
                          { selectedStyle.length>1 &&
                          <Typography variant="subtitle2" color="black" >{selectedStyle}</Typography>
                          || 
                          <Typography variant="subtitle2" color="black" >Select Style</Typography>
                          }

                          <span>
                              <KeyboardArrowDownIcon/>
                          </span>
                      </Dropdown_Button>
                      {isActive &&
                      <Dropdown_Content>

                          {styleSelection.map((option) => (
                              <Dropdown_Item 
                                  key={option}
                                  onClick={(e) => {
                                  setselectedStyle(option)
                                  setisActive(false)
                                  updateStyle(dispatch, option)
                              }}
                              >
                                <Typography variant="subtitle5" color="black" sx={{ "&:hover": { color: "white" } }}>{option}</Typography>
                              </Dropdown_Item>
                          ))}
                      </Dropdown_Content>
                    }

使用的函数如下:

export const updateStyle = async (dispatch, option) => {
  dispatch(searchUpdateStyle(option));
}

我做错了什么?

vlurs2pr

vlurs2pr1#

searchUpdateStyle需要3个参数:(状态、动作、选项)
您只传递了一个参数,它将被视为第一个参数,在您的示例中为“state”

相关问题