我試圖理解為什么我似乎無法將參數傳遞給選擇器。本質上:我有一個存儲數字和布爾值的邏輯 - 該數字是我想要選擇材料元素的參數 - 我有一個調度操作的按鈕://on a jobcard - dispatches to the material logic slice, pushing the number into the redux state: materialforJobNumber; <button className="materialViewer" onClick={() =>{dispatch(materialView(data.jobsessionkey))}}> MATERIAL</button>它分派到邏輯片://logic slicematerialView:(state, action)=>{ console.log("what"); console.log("material view succesfully called", action); const newState = {...state}; console.log(newState); newState.materialforJobNumber = action.payload; newState.showMaterial=true; return newState},//the component that handles displaying the list of material for a job;import {selectAttachedByJobKey} from "../MaterialCard/MaterialSlice";const store= useStore();const foundJN = store.getState().logic.materialforJobNumber;const selectedJob=store.getState().jobs.find(x=>x.jobsessionkey==foundJN);const materials = useSelector(selectAttachedByJobKey(foundJN));const materialCards = materials.filter(x=>x.AttachedJobKey==foundJN).map((x,i)=><MaterialCard props={x} key ={i} />)//material slice:import { createSlice } from '@reduxjs/toolkit';export const selectAttachedByJobKey = (state,action) =>{ console.log("selecting material by job key",action); console.log(action?.payload); console.log(state); return state.material.filter(x=>x.AttachedJobkey===action?.payload);}我試圖通過單擊按鈕來顯示“工作”的相關材料。我正在使用 createReducer 函數模式,但我對如何實現這一點有點迷失 - 是否有更干凈的方法來使用帶有屬性的選擇器?
React redux useSelector 和參數
有只小跳蛙
2023-12-14 15:13:41