21
loading...
This website collects cookies to deliver better user experience
const Component = ({ linkUrl }) => (
<div>
{ !!linkUrl && <PrettyLink url={linkUrl} /> }
</div>
}
// -- OR --
const Component = ({ linkUrl }) => {
return (
<div>
{linkUrl !== undefined && linkUrl !== null ? (
<PrettyLink url={linkUrl} />
) : null}
</div>
);
};
&& operator
means that one branch is not explicitly stated, so it's up to the reader to figure out what the expected behavior is for the other branch (even if it's to render nothing), whether it was left out by mistake, and look for information that is not provided to them.const idObjects = ids.reduce((prev, curr) => {
return {
...prev,
[curr]: {
id: curr,
value: getValueFromId(id),
}
};
}, {});
Array.reduce
function is often used to convert an array into a different data structure, like an object. The code is very compact, but it's also often difficult to understand — there's a lot of details to keep track of:reduce
callconst Component = ({ hasImage }) => {
// ...
return (
<div>
{hasImage && <Image />}
</div>
);
}
&&
operator. Based on the value of the left-hand side operand, the right-hand side operand might be rendered.&&
expression is evaluated and the left-hand side operator evaluates to a falsy value, then that operand is returned and the evaluation of the right-hand side operand is entirely skipped.useState
, useReducer
, useEffect
, and useLayoutEffect
, and the ability to reuse logic in the form of custom Hooks. While these tools allow React developers to handle flows very easily and effectively, they also have their drawbacks in certain scenarios.useEffect
Hook is not an uncommon occurrence.const Component = ({ data }) => {
// Logic...
// Here, we're combining flows for the data request, empty state, filled logic state, and
// server error feedback into a single `useEffect`. It feels natural, but is it readable?
useEffect(() => {
if (!data) {
setRequestState("LOADING");
} else if (data.length === 0) {
setRequestState("DONE");
triggerEmptyState();
} else {
setRequestState("DONE");
if (dataIsValid(data)) {
updateOtherLogicWithData(data);
} else {
informServerDataIsInvalid();
}
}
}, [data, updateOtherLogicWithData, informServerDataIsInvalid, triggerEmptyState]);
// Render...
}
useEffect
Hook like this is that it can negatively influence code readability. Putting different flows closely together will make them intertwined, difficult to separate, and tightly coupled. The resulting code will thus become more difficult to understand and harder to maintain.valid
, consider naming it isValid
; instead of validate
, consider calling it onValidate
isLoading
and an isError
boolean prop
Avatar
, but if it's specifically meant to be used in a section of a card, then it's beneficial to make that explicit in the name and call it CardAvatar
onChange
prop, then naming that callback onChange
will not add any useful information to the reader
updateStateValue
increases the readability because it clarifies what the callback does and what will happen when the appropriate change event occurs in the used componentconst SomeSection = ({ isEditable, value }) => {
if (isEditable) {
return (
<OuterSection>
<Header>Edit this content</Header>
<Body>{value}</Body>
<SectionButton>Clear content</SectionButton>
</OuterSection>
);
}
return (
<OuterSection>
<Header>Read this content</Header>
<Body>{value}</Body>
</OuterSection>
);
}
// -- OR --
const SomeSection = ({ isEditable, value }) => {
return (
<OuterSection>
<Header>
{ isEditable ? "Edit this content" : "Read this content"}
</Header>
<Body>{value}</Body>
{ isEditable ? <SectionButton>Clear content</SectionButton> : null }
</OuterSection>
);
}