第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

React redux useSelector 和參數(shù)

React redux useSelector 和參數(shù)

有只小跳蛙 2023-12-14 15:13:41
我試圖理解為什么我似乎無法將參數(shù)傳遞給選擇器。本質(zhì)上:我有一個(gè)存儲數(shù)字和布爾值的邏輯 - 該數(shù)字是我想要選擇材料元素的參數(shù) - 我有一個(gè)調(diào)度操作的按鈕://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);}我試圖通過單擊按鈕來顯示“工作”的相關(guān)材料。我正在使用 createReducer 函數(shù)模式,但我對如何實(shí)現(xiàn)這一點(diǎn)有點(diǎn)迷失 - 是否有更干凈的方法來使用帶有屬性的選擇器?
查看完整描述

1 回答

?
慕無忌1623718

TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊

useSelector僅接受一個(gè)函數(shù)state作為參數(shù)。如果你想傳遞其他變量,你可以使用柯里化方法:


export const selectAttachedByJobKey = (action) => (state) =>{

    console.log("selecting material by job key",action);

    console.log(action?.payload);

    console.log(state);

    return state.material.filter(x=>x.AttachedJobkey===action?.payload);

}

現(xiàn)在,當(dāng)您這樣做時(shí),const materials = useSelector(selectAttachedByJobKey(foundJN));您將傳遞給可用的useSelector函數(shù)foundJN。


查看完整回答
反對 回復(fù) 2023-12-14
  • 1 回答
  • 0 關(guān)注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號