react js Material-ui에서 칩 삭제
Sep 20 2020
react-js 및 Material-ui 칩으로 작업했습니다. 많은 칩을 추가 한 다음 텍스트와 함께 칩이 표시됩니다. 칩을 삭제하는 기능을 갖고 싶습니다. 하지만 어떻게 삭제할 수 없습니다.
어떻게 삭제할 수 onDelete={}있습니까?
다음은 내 코드의 일부입니다.
import React, { useEffect, useState, Component } from 'react'
import Chip from '@material-ui/core/Chip'
const useStyles = makeStyles(theme => ({
root: {
display: 'flex'
},
chip: {
backgroundColor: '#104d56'
},
}))
const handleDelete = () => {
console.log(hashtag);
};
const studentlist = []
function ResponsiveDrawer(props) {
{
namefamily.map((item, index) => {
if (index + 1 > namefamily.length - gpcapacity) {
if (showstu == item[2]) {
studentlist.push(
<Chip
key={item[2]}
className={classes.chip}
clickable={false}
variant="outlined"
label={
<Typography variant="caption" color="textSecondary">
{item[0]} {item[1]}
</Typography>
}
onDelete={handele}
icon={<FaceIcon />}
/>
)
} else
studentlist.push(
<Chip
key={item[2]}
variant="outlined"
clickable={true}
label={
<Typography variant="caption" color="textSecondary">
{item[0]} {item[1]}
</Typography>
}
icon={<FaceIcon />}
/>
)
}
})
}
}
handleDelete()Function 내부에는 무엇이 있습니까?
답변
1 PrasadPhule Sep 20 2020 at 19:15
내 이해대로
namefamily배열 즉, 배열입니다[['a','b','c'],['x','y','z']]if (index + 1 > namefamily.length - gpcapacity) {조건을 사용하여 일부 학생 칩을 건너 뛰고 싶습니다.- 조건을
showstu사용하여 학생에게 칩 삭제 허용showstu == item[2]
정답 : 당신 ResponsiveDrawer은 다음과 같습니다
자세한 내용은 코드의 내 주석을 참조하십시오.
const ResponsiveDrawer = () => {
//considering you are getting showstu and gpcapacity as props or from somewhere
const classes = useStyles();
// point 2. you can skip here instead of checking if everytime
const [chipData, setChipData] = React.useState(
namefamily.slice(0, ((namefamily.length - 2) - gpcapacity))
);
const handleDelete = (chipToDelete) => {
setChipData((chips) => chips.filter((chip) => chip[2] !== chipToDelete));
};
return (
chipData.map((item, index) => {
let chipProps = { clickable:true };
//point 3. allowing delete to those who is {showstu}
if (showstu == item[2]) {
chipProps.onDelete = () => handleDelete(item[2]);
chipProps.className = classes.chip;
chipProps.clickable = false;
}
return (
<Chip
//for key better to use index as a unique
key={index}
// this is spread operator (https://reactjs.org/docs/jsx-in-depth.html#spread-attributes)
{...chipProps}
variant="outlined"
label={
<Typography variant="caption" color="textSecondary">
{item[0]} {item[1]}
</Typography>
}
icon={<FaceIcon />}
/>
);
})
);
}