简体   繁体   English

从映射数组中过滤掉 onClick

[英]filtering out onClick from a mapped array

I am trying to figure out how to filter out a mapped array and making the rest of the results disappear in the same component.我试图弄清楚如何过滤出映射数组并使结果的 rest 在同一组件中消失。 I've done the same with React Router as I can route the result to a different page but I am wondering if there is a way to do the same on the same component?我对 React Router 做了同样的事情,因为我可以将结果路由到不同的页面,但我想知道是否有办法在同一个组件上做同样的事情? I have a Directory component (below) that is mapping through an array to display results of items on the page.我有一个目录组件(如下),它通过数组映射以显示页面上项目的结果。

I would like to click on one of the elements and remove the rest.我想单击其中一个元素并删除 rest。 I tried to incorporate a filter method in the same component but drawing blanks on how I should implement it.我试图在同一个组件中合并一个过滤器方法,但在我应该如何实现它上画了个空白。 Let me know what you think!让我知道你的想法!


import React from 'react'
import { Card, CardImg} from 'reactstrap'

function Presentational({example, onClick}){
    return(
        <Card onClick={()=> onClick(example.name) }>
            <CardImg src={example.image}/>
        </Card>
    )
}

function Directory(props){

    const examples = props.propExample.map(example=>{
        return (
            <div>
                <Presentational example={example} onClick={props.onClick} />
            </div>
        )
    })

    return(
        <div>
            {examples}
        </div>
    )
}

export default Directory;

You may use useState hook for selection您可以使用 useState 钩子进行选择

We store clicked elements inside the state variable selected.我们将点击的元素存储在选定的 state 变量中。 using useState hook.使用 useState 钩子。 When the user clicks on the element react component will remember which element he clicked and will render an array from 1 clicked element [selected] .当用户单击元素时,react 组件将记住他单击了哪个元素,并将从 1 个单击的元素[selected]呈现一个数组。

In order to cleanup selection, just call setSelected()为了清理选择,只需调用setSelected()

It is the same logic as you want.这与您想要的逻辑相同。

import React, {useState} from 'react'
import { Card, CardImg} from 'reactstrap'

function Presentational({example, onClick}){
    return(
        <Card onClick={()=> onClick(example.name) }>
            <CardImg src={example.image}/>
        </Card>
    )
}

function Directory(props){
    const [selected, setSelected] = useState()
    const examples = (selected ? [selected] : props.propExample).map(example=>{
        return (
            <div>
                <Presentational example={example} onClick={(name) => {
                     props.onClick(name)
                     setSelected(example)
                  }}
                />
            </div>
        )
    })

    return(
        <div>
            {examples}
        </div>
    )
}

export default Directory;

if you want to do it with a filter clause it will look almost the same, but with the extra operations如果你想用过滤子句来做,它看起来几乎一样,但有额外的操作

import React, {useState} from 'react'
import { Card, CardImg} from 'reactstrap'

function Presentational({example, onClick}){
    return(
        <Card onClick={()=> onClick(example.name) }>
            <CardImg src={example.image}/>
        </Card>
    )
}

function Directory(props){
    const [selected, setSelected] = useState()
    const examples = props.propExample.filter(it => typeof selected === 'undefined' || it.name === selected).map(example=>{
        return (
            <div>
                <Presentational example={example} onClick={(name) => {
                     props.onClick(name)
                     setSelected(name)
                  }}
                />
            </div>
        )
    })

    return(
        <div>
            {examples}
        </div>
    )
}

export default Directory;

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

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