I have researched this topic for quite some time now, yet I still cannot make the ‘Remove’ button in the child component (ControlledOpenSelect) only remove the item with the key it was passed – by using the callback function.
My ControlledOpenSelect (the child component):
JavaScript
x
76
76
1
const example={'skittle':10,"carrots":20,"cars":50,"potatoes":30}
2
3
export default function ControlledOpenSelect({ourObject=example,onRemove,key}) {
4
const classes = useStyles();
5
6
const [open, setOpen] = React.useState(false);
7
const [product,setProduct]=React.useState('None')
8
const [quantity,setQuantity]=React.useState(0)
9
const [price,setPrice]=React.useState('')
10
const [subTotal,setSubTotal]=React.useState(0)
11
12
const handleChange = (event) => {
13
setProduct(event.target.value);
14
};
15
16
17
const handleClose = () => {
18
setOpen(false);
19
};
20
const handleOpen = () => {
21
setOpen(true);
22
};
23
24
const handleQuantity=(event)=>{
25
setQuantity(event.target.value)
26
}
27
28
29
30
//const productList=Object.keys(ourObject)
31
32
//const correct_price=ourObject[product]
33
34
//React.useEffect(()=>{
35
//setPrice(correct_price)
36
//},[correct_price])
37
38
39
//React.useEffect(()=>{
40
//setSubTotal(price*quantity)
41
//},[quantity,product,price])
42
43
44
45
46
return (
47
<div>
48
<Button className={classes.button} onClick={handleOpen}>
49
Open the select
50
</Button>
51
<FormControl className={classes.formControl}>
52
<InputLabel id="demo-controlled-open-select-label">Product</InputLabel>
53
<Select
54
labelId="demo-controlled-open-select-label"
55
id="demo-controlled-open-select"
56
open={open}
57
onClose={handleClose}
58
onOpen={handleOpen}
59
value={product}
60
onChange={handleChange}
61
>
62
{productList.map(
63
item => <MenuItem value={item}>{item}</MenuItem>
64
)}
65
</Select>
66
<div>
67
<TextField id="outlined-basic" label="Quantity" variant="outlined" onChange={handleQuantity}/>
68
<TextField id="outlined-basic" label="Price" variant="outlined" value={price} />
69
<p>{subTotal}</p>
70
</div>
71
</FormControl>
72
<button onClick={()=>onRemove(key)}>Remove</button>
73
</div>
74
);
75
}
76
My parent component FullComponent:
JavaScript
1
36
36
1
const example={'skittle':10,"carrots":20,"cars":50,"potatoes":30}
2
3
4
const FullComponent=({ourObject=example})=>{
5
6
const [add,setAdd]=React.useState([])
7
// const [remove,setRemove]=React.useState([])
8
9
const id=React.useState(_uniqueId('prefix-'));
10
11
const handleClick=(event)=>{
12
setAdd([add,
13
<ControlledOpenSelect ourObject={ourObject} id={id}/>])
14
}
15
16
const handleRemove=(id)=>{
17
const newAdd=add.filter((item)=> item.id !== id)
18
setAdd(newAdd)
19
}
20
21
return (
22
<>
23
{add.map((item)=>{
24
return (
25
<>
26
<ControlledOpenSelect ourObject={ourObject} key={item.id} onRemove={handleRemove} />
27
</> )
28
})}
29
<button type="button" onClick={handleClick}>Add</button>
30
</>
31
)
32
}
33
34
35
export default FullComponent
36
Thank you so much!
Advertisement
Answer
You are not passing the id to the handleRemove method . you need to pass an inline function which passes the item.id as argument
JavaScript
1
2
1
onRemove={() => handleRemove(item.id)}
2