简体   繁体   English

当用户在 react.s 中选择来自 api 的列表项时,如何切换 class?

[英]How to toggle class when user selects a list item coming from api in react.s?

I'm working on an application where I render the DB results by a loop through using the map method.我正在开发一个应用程序,在该应用程序中,我通过使用 map 方法循环呈现数据库结果。 When the user clicks on each item, first it will add those id's into an array and toggle a CSS class to change the border color of a particular selected item, when the item is selected again, the border will disappear, same goes for all items rendered in the UI.当用户点击每个项目时,首先它会将这些 id 添加到一个数组中并切换 CSS class 以更改特定选定项目的边框颜色,当再次选择该项目时,边框将消失,所有项目都一样在 UI 中呈现。 Adding the id's into an array is working fine.将 id 添加到数组中工作正常。

I'm facing a problem toggling CSS style to change the border color when click on each item.单击每个项目时,我在切换 CSS 样式以更改边框颜色时遇到问题。

Here is my code to render items on DOM这是我在 DOM 上渲染项目的代码

const [selectedItems, setSelectedItems] = React.useState([])

const onClickHandler = (id) => {
    if (selectedItems.indexOf(id) > -1) {
        let updatedItems = selectedItems.filter(itemID => itemID !== id)
        console.log(updatedItems);
        setSelectedItems(updatedItems)
    } else {
        setSelectedItems(prevState => [...prevState, id] )
    }
    setOpen(true);
}
    
{
    courses.length > 0 && courses.map(course => {
        return (
            <>
                <CourseItem course={course} onClickHandler={onClickHandler} itemSelect={itemSelect} />
            </>
        )
    })
}

function CourseItem({ course, onClickHandler }) {
    const classes = useStyles();
    return (
        <>
             <Col style={{ marginTop: 10 }}>
                    <Paper elevation={0}  className={classes.card}>
                        <div className={classes.cardTop} style={{  backgroundImage: `url('${course.thumbnail}')`, backgroundSize: 'cover', backgroundPosition: 'center' }}>
                            <div style={{ padding: '8px 18px', display: 'flex', justifyContent : 'space-between' }}>
                                <CheckCircleIcon  onClick={() => onClickHandler(course._id)} />
                                {/* <Typography component={"label"} variant="caption"> { course.level }</Typography> */}
                                    <Chip label={course.level} style={{ color: '#000', backgroundColor: '#f9f9f9', fontFamily: 'regular1', position: 'relative', zIndex: 0}} variant="outlined" />
                            </div>
                        </div>
                        <div className={classes.cardBottom}>
                            <Typography> { course.moduleName }</Typography>
                            <Typography variant="subtitle2" component={"span"}> { course.description }</Typography>
                        </div>
                    </Paper>
             </Col>
        </>
    )
}

You can use tow different class for two different condition.对于两种不同的情况,您可以使用两个不同的 class。 Then use clsx to conditionally apply class.然后用clsx有条件的申请class。

clsx link -> npm clsx clsx 链接 -> npm clsx

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

相关问题 使用React.s单击内容时不要关闭模式 - Don't close the modal when click on content with React.s 用户从列表中选择时如何显示内容详细信息? - How to show content details when user selects from list? 当没有来自 API 的数据时,如何在 React 中向用户显示错误消息 - How to show an error message to the user in React when no data coming from API 当用户从下拉菜单中选择项目时,如何防止引导程序模式关闭 - How to prevent a bootstrap modal from closing when user selects item from dropdown-menu 当用户从 React 的下拉列表中选择一个选项时如何启用另一个输入字段 - How to enable another input field when a user selects an option from dropdown in React React:当用户从下拉菜单中选择一个值时,如何更新 object 的 state? - React: How to update the state of an object when a user selects a value from a drop-down menu? 如何在 React Native 中切换来自地图迭代的单个元素? - How to toggle a single element coming from a map iteration in react native? 当用户从日期选择器中选择时,使用AJAX更新API URL - Update an API URL using AJAX when user selects from datepicker jQuery列表项类切换 - jquery list item class toggle 在地图功能中反应一个项目的切换类(但是当另一个将被放置时,将其从另一个中删除) - React toggle class in map fuction for one item(but when another will be put remove from anothers)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM