-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCUpgradeSpecsUsingHocReact.jsx
More file actions
71 lines (48 loc) · 3.1 KB
/
PCUpgradeSpecsUsingHocReact.jsx
File metadata and controls
71 lines (48 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Challenge
// You're building a new feature on your e-commerce site which displays two computer specs - basic and pro.
// The PcDisplay component is already built and expects 5 props. { title, price, cpu, ram, ssd }
// You will need to build a withPriceModel HOC and using that to manage the price of the BasicModel and ProModel components.
// Build a HOC called withPriceModel which takes the PcDisplay component for first param and price increase number for the second param.
// withPriceModel will manage the price and must set a default price of 50.
// BasicModel should use the default price set by withPriceModel
// ProModel should use withPriceModel to increase the price by 60. Total price should be 110.
// Since the withPriceModel is responsible for managing the price, ensure that it can't be overritten by passing in a price prop.
const React = require('react');
// Don't change PcDisplay
const PcDisplay = (props) => {
return (<div>
<h1>{props.title}</h1>
<p id="price">£{props.price}</p>
<ul>
<li><label>CPU</label> <span>{props.cpu}</span></li>
<li><label>RAM</label> <span>{props.ram}</span></li>
<li><label>SSD</label> <span>{props.ssd}</span></li>
</ul>
</div>);
};
// Implement HOC -> returns a functions that wraps the passed in `PcDisplay` component
let withPriceModel = ((Component, increase) => {
return function(props) {
const newPrice = 50 + increase || 50
const newProps = {...props, price: newPrice}
return <Component {...newProps} />
}
});
// In this case we could also deconstruct the price from our props and use that instead
// something like: const { price, ...rest } = props;
// then just make it price afterwards: return <WrappedComponent price={newPrice} {...rest} />;
// Build basic and pro model components using `withPriceModel`
let BasicModel = withPriceModel(PcDisplay);
let ProModel = withPriceModel(PcDisplay, 60);
/*
So in this problem we do HOC, which is bascially higher order functions but with components obviously. It looks confusing at first
but its not as bad as it seems. The first component is already set as PcDisplay and we will be using that. All we want to do
is add a price to it. One being a basic model with the price being 50 flat and one with the pro model which the price increase
can vary. so we need to set a base withPriceModel to work from. we do this by passing in the component, and the price.. we called
it increase here but price would fit symatically as well. use "return function(props)" to access the props from the compoent.
then we set our new price to be 50 + increase. if increase doesnt exist we just set to 50 for the base. then we need to pass our
new prop with the component out. So we create new props with a spread syntax and add price to it. Think of this withPriceModel as a
roadside pitstop that is upgrading our compoent with an extra shiny new prop. once the component is passed out. we can then set a
basic model that, if we dont pass a price increase, will have a base price of 50. if pass a price like we do in the proModel,
then that will increase the base. so in this proModel case, the price would be 110.
*/