There is a React component –
‘function Product (props) {
JavaScript
x
48
48
1
const {
2
prod_id: id,
3
prod_name : title,
4
prod_status: status,
5
prod_price: price,
6
prod_oldprice : oldPrice,
7
} = props;
8
9
10
let oldPriceChecker = (oldPriceValue) => {
11
if (oldPriceValue) {
12
let oldPriceStr = oldPriceValue + ' zł';
13
return(oldPriceStr);
14
}else {
15
return('');
16
}
17
}
18
19
20
let statusChecker = (value) => {
21
if (value != undefined){
22
let string = value;
23
let array = string.split(',');
24
console.log(array);
25
array.map(n => <div className="status">{n}</div>)
26
}
27
}
28
29
30
31
return (
32
33
<div className="row">
34
<div className="card">
35
<div className="card-image">
36
<img src="https://via.placeholder.com/150" />
37
</div>
38
<div className="card-content">
39
<span className="card-title">{title}</span>
40
<hr className="card_hr" />
41
<p className="card_price" >{price} zł</p>
42
<div className="card_price old_price">{oldPriceChecker(oldPrice)}</div>
43
{statusChecker(status)}
44
</div>
45
</div>
46
47
)
48
}
export {Product}
Question: The variable prod_status: status can contain several values (for example, “new,promotion”, if so, you need to split it into two words and create a separate block for each, since now the block comes with the whole line
It is necessary like this (new, saleout, etc. in separate blocks)
I tried to create a function but it just outputs an array to the console
I think I’m not using the property “map” correctly
Advertisement
Answer
The problem:
The function you have created statusChecker
does not return anything. Therefore when you want to print it ({statusChecker(status)}
) it doesn’t do anything.
JavaScript
1
11
11
1
let statusChecker = (value) => {
2
if (value != undefined){
3
let string = value;
4
let array = string.split(',');
5
console.log(array);
6
7
//This is what you want to return but is not returned
8
array.map(n => <div className="status">{n}</div>)
9
}
10
}
11
Solution:
return the mapped array from the function.
JavaScript
1
11
11
1
let statusChecker = (value) => {
2
if (value != undefined){
3
let string = value;
4
let array = string.split(',');
5
console.log(array);
6
7
//This is what you want to return but is not returned
8
return array.map(n => <div className="status">{n}</div>)
9
}
10
}
11