简体   繁体   English

如何触发设置在变量中的过滤器 function?

[英]How to fire filter function that's set in a variable?

Hello i have a filter function that's set in a variable, but want to execute as soon as the user clicks the button.您好,我有一个过滤器 function 设置在变量中,但希望在用户单击按钮后立即执行。 The function enableFilter has to check if the item.id matches the item.category and fire the filter as it is true. function enableFilter 必须检查 item.id 是否与 item.category 匹配并触发过滤器,因为它是真的。 Can anyone help me with that?任何人都可以帮助我吗? It is not necessary to filter two at a time.不必一次过滤两个。 Only one filter at a time一次只能过滤一个

You can store your filter condition id in a state variable, like this:您可以将过滤条件 ID 存储在 state 变量中,如下所示:

const [selectedItemId, setSelectedItemId] = useState(null);

Then change your filter cases based on selectedItemId , like:然后根据selectedItemId更改您的过滤器案例,例如:

const filteredCases = i18n('casesOverview.cases').filter(category => selectedItemId ? category.category.includes(selectedItemId) : true)

then update the button onClick props with setSelectedItemId as follows:然后使用setSelectedItemId更新按钮onClick道具,如下所示:

{i18n('casesOverview.categories').map((item, key) => (
              <ButtonOutline
                onClick={() => setSelectedItemId(item.id)}
                id={item.id}
                key={key}>{item.label}</ButtonOutline>
            ))
}
 <GridWideColumn>
     {filteredCases.map((item, key) => (
                <CardBrand
                  key={key}
                  text={item.teaser} href={item.link}
                  image={<Image src="" srcSet={item.coverImage} alt="" />}
                  brandImage={<Image src={item.clientLogo} alt="" />}
                  link={item.link}
                  client={item.client}
                  textInfo={item.category}
                />
      ))
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM