New to react world, trying to learn destructuring, have been reading about it but stuck here, if i do it like this function MList({action}) { // const data = [action];} i am just getting ‘cameras’. So how to destructure and get same result as with props below this is Mcard.js:
JavaScript
x
5
1
<Box pt={1}>
2
<MList
3
action="cameras"
4
/>
5
</Box>
This is inside MList komponent:
i want to destructure this code ( works gives ‘name’ and ‘ident’):
JavaScript
1
16
16
1
function MList(props) {
2
3
const initialize = () => {
4
const data = props[props.action];
5
6
if (!data || data.length < 1) {
7
return;
8
}
9
data.map((e) => {
10
collapseStates["" + e.name + e.ident] = false;
11
return;
12
});
13
setCollapseS(collapseS);
14
};
15
16
}
Advertisement
Answer
I don’t know React but destructuring the arguments should be something like the following
JavaScript
1
17
17
1
function MList({action, tail}) {
2
3
const initialize = () => {
4
const data = tail[action];
5
6
if (!data || data.length < 1) {
7
return;
8
}
9
data.map(({name, ident}) => {
10
collapseStates["" + name + ident] = false;
11
return;
12
});
13
setCollapseS(collapseS);
14
};
15
16
}
17
Also I would suggest using data.forEach
instead of data.map
if you don’t need to save the result in another array