Imagine you have several blocks of similar layout which are hard-coded on the frontend side. These blocks are not dynamic, they are not being fetched from back-end, it’s just a layout. Most beginners, when they see similar blocks of layout, start to think about arrays as a tool to handle similar stuff. So they often do something that is shown in the following snippet (the example might not be the best):
const cargoProperties = [
{ label: "Cargo type", content: "Containers, Grand Cube 40, Pallete" },
{ label: "Vehicle type", content: "Container Truck" },
{ label: "Size", content: "7 m" },
{ label: "Weight", content: "22kg/m3" },
{ label: "Qiantity", content: "5" },
{ label: "Dangerous", content: "no" },
{ label: "License plate", content: "#SR-045-JD" },
{
label: "Notes:",
content: "Here you can see some notes from customer",
},
];
const Layout = ({ styles }) => (
<div>
{cargoProperties.map((property) => (
<RideProperty key={property.label} label={property.label}>
{typeof property.content === "string" ? (
<Text numberOfLines={1} style={styles.textProperty}>
{property.content}
</Text>
) : (
<View style={styles.viewProperty}>{property.content}</View>
)}
</RideProperty>
))}
</div>
);
What are the key problems of the code above?
cargoProperties
. JSX is much more expressive and readable than JSON and React gives us the power of JSX to code our layout so why not to use it to the fullest. .map().
<Text>
and <View>
for each item in the list on each <Layout>
update. This might not be an issue in the case above. But in the future this could be a performance bottleneck.// `styles` prop comes from the HOC
const Row = ({ children, label, styles }) => (
<RideProperty label={label}>
{typeof children === "string" ? (
<Text numberOfLines={1} style={styles.textProperty}>
{children}
</Text>
) : (
<View style={styles.viewProperty}>{children}</View>
)}
</RideProperty>
);
const Layout = () => (
<div>
<Row label="Cargo type">Containers, Grand Cube 40, Pallete</Row>
<Row label="Vehicle type">Container Truck</Row>
<Row label="Size">7 m</Row>
<Row label="Weight">22kg/m3</Row>
<Row label="Qiantity">5</Row>
<Row label="Dangerous">no</Row>
<Row label="License plate">#SR-045-JD</Row>
<Row label="Notes:">Here you can see some notes from customer</Row>
</div>
);
What are the benefits of the solution above:
<Row>
component now.<Row>
component can now be optimized using React.memo()
or React.PureComponent
.<Row>
you want.Let me know how you would refactor it in the comments section.