1 回答

TA貢獻1891條經驗 獲得超3個贊
那么您想從兩個選擇器獲取狀態并做一些工作,然后返回結果?這是 的完美問題類型reselect。Reselect 是一個幫助程序,可讓您記住有關狀態選擇器的昂貴計算。
https://github.com/reduxjs/reselect
這對你來說可能是這樣的。
$ yarn add reselect
import React from 'react';
import { useSelector } from 'react-redux';
import { createSelector } from 'reselect';
const filterArraySelector = (state) => state.filterArray;
const doctorsSelector = (state) => state.doctors;
const filteredDoctorsSelector = createSelector(doctorsSelector, filterArraySelector, (filterArray, doctors) => {
? return doctors.filter((doctor) => {
? ? return filterArray.all((key) => {
? ? ? // Do some comparison here, return true if you want to include the doctor in the results
? ? ? return doctor[key] !== undefined;
? ? });
? });
});
const Results = () => {
? const filteredDoctors = useSelector(filteredDoctorsSelector);
? return filteredDoctors.map((doctor) => <span>{doctor}</span>);
};
替代方案
您可以在每次渲染時簡單地過濾醫生,而不是使用createSelector記憶過濾。像這樣:
const Results = () => {
? const filterArray = useSelector((state) => state.filterArray);
? const doctors = useSelector((state) => state.doctors);
? const filteredDoctors = useMemo(
? ? () =>
? ? ? doctors.filter((doctor) => {
? ? ? ? return filterArray.all((key) => {
? ? ? ? ? // Do some comparison here, return true if you want to return the doctor
? ? ? ? ? return doctor[key] !== undefined;
? ? ? ? });
? ? ? }),
? ? [doctors, filterArray]
? );
? return filteredDoctors.map((doctor) => <span>{doctor}</span>);
};
給定一個像這樣的值的filterArray:
const filterArray = ['Female', 'English'];
我們可以更新過濾器函數來根據 filterArray 值測試對象值。如果任何屬性值與filterArray值匹配,那么我們可以將醫生包含在生成的filteredDoctors列表中。
const Results = () => {
? const filterArray = useSelector((state) => state.filterArray);
? const doctors = useSelector((state) => state.doctors);
? const filteredDoctors = useMemo(() => {
? ? return doctors.filter((doctor) => {
? ? ? return filterArray.some((filter) => {
? ? ? ? return Object.values(doctor).some((value) => value === filter);
? ? ? });
? ? });
? }, [doctors, filterArray]);
? return filteredDoctors.map((doctor) => <span>{doctor}</span>);
};
更新:
const Results = () => {
? const filterArray = useSelector((state) => state.filterArray);
? const doctors = useSelector((state) => state.doctors);
? const filteredDoctors = useMemo(() => {
? ? return doctors.filter((doctor) => {
? ? ? return filterArray.some((filter) => {
? ? ? ? return Object.values(doctor).some((value) => {
? ? ? ? ? // If the attribute value is an array
? ? ? ? ? if (Array.isArray(value)) {
? ? ? ? ? ? return value.some((value) => value === filter);
? ? ? ? ? }
? ? ? ? ? // If the attribute value is an object, get the values from the object
? ? ? ? ? if (typeof value === 'object') {
? ? ? ? ? ? return Object.values(value).some((value) => value === filter);
? ? ? ? ? }
? ? ? ? ? // By default, expect the value to be a string
? ? ? ? ? return value === filter;
? ? ? ? });
? ? ? });
? ? });
? }, [doctors, filterArray]);
? return filteredDoctors.map((doctor) => <span>{doctor}</span>);
};
添加回答
舉報