Created
May 17, 2020 05:05
-
-
Save zzdjk6/d04ac98f5599a10e12b3f6bb5461d891 to your computer and use it in GitHub Desktop.
React Hooks Solution #EarlyReturn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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