Skip to content

Instantly share code, notes, and snippets.

@zzdjk6
Created May 17, 2020 05:05
Show Gist options
  • Save zzdjk6/d04ac98f5599a10e12b3f6bb5461d891 to your computer and use it in GitHub Desktop.
Save zzdjk6/d04ac98f5599a10e12b3f6bb5461d891 to your computer and use it in GitHub Desktop.
React Hooks Solution #EarlyReturn
const MyComponent: React.FC = () => {
const data1 = useData1();
const data2 = useData2(data1);
const result = React.useMemo(() => {
if (!data1 || !data2) {
return null;
}
return aggregateData(data1, data2);
}, [data1, data2]);
if (!result) {
return null;
}
return <ResultDisplay result={result} />;
};
// Add null check for data1 inside useData2
function useData2(data1) {
if (!data1) {
return null;
}
return getData2(data1.id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment