Skip to content

Instantly share code, notes, and snippets.

View Pyrolistical's full-sized avatar

Pyrolistical

View GitHub Profile
@Pyrolistical
Pyrolistical / async and await.js
Created April 13, 2020 19:43
Sophek Tounn question
try {
const audioDataCount = await axios.post(netlifyConfig().DOMAIN + 'get-scene-audio-count', {
email: value.email,
sourceID: value.sourceID,
source: value.source
})
console.log('audioDataCount', audioDataCount)
const res = {
count: audioDataCount.data.audioScenesList.count,
data: audioDataCount.data.audioScenesList.items
type ApexLegendsGameResult = {
teamID: string,
placement: number,
kills: number
};
application.post('/apex-legends/:gameID/report-score', async ({params: {gameID}, body}, res) => {
await db.collection('apexLegendsGames').updateOne({
_id: gameID
}, {
type ApexLegendsGameResult = {
teamID: string,
placement: number,
kills: number
};
application.post('/apex-legends/:gameID/report-score', async ({params: {gameID}, body}, res) => {
const results: ApexLegendsGameResult[] = body;
await db.collection('apexLegendsGames').updateOne({
_id: gameID
}, {
type ApexLegendsGameResult = {
teamID: string,
placement: number,
kills: number
};
application.post('/apex-legends/:gameID/report-score', async ({params: {gameID}, body}, res) => {
await db.collection('apexLegendsGames').updateOne({
_id: gameID
}, {
$set: {
// BEFORE
const [state, setState] = useState();
useEffect(() => {
setState(calculateInitialState());
}, []);
// AFTER
const [state, setState] = useState(calculateInitialState);
@Pyrolistical
Pyrolistical / useMemo instead.js
Created October 2, 2021 06:12
https://blog.battlefy.com/how-to-escape-react-hooks-hell-a66c0d142c9e Unnecessary useEffect and useState for computed value based on prop
// BEFORE
const [costlyValue, setCostlyValue] = setState();
useEffect(() => {
setCostlyValue(computedCostlyValue(props.someParam));
}, [props.someParam]);
// AFTER
const costlyValue = useMemo(() => computedCostlyValue(props.someParam), [props.someParam]);
const BrokenComponent = (props) => {
useEffect(() => {
console.log('BrokenComponent called useEffect');
}, [props]); // THIS IS ALWAYS WRONG!
return <code>{JSON.stringify(props)}</code>;
};
const GoodComponent = (props) => {
useEffect(() => {
// Good useMemo usage
const NormalComponent = ({ count }) => {
const costlyValue = useMemo(() => costlyFunction(count), [count]);
return <p>NormalComponent {costlyValue}</p>;
};
export default ({ count }) => {
// Slow component
const SlowComponent = () => {
const costlyValue = costlyFunction(count);
// BEFORE
const App = () => {
const [locale, setLocale] = useState('english');
return <Navigation locale={locale} />;
};
const Navigation = ({ locale }) => {
return <Link locale={locale} href="/about">About</Link>;
};
// BAD
export default ({ count }) => {
const Label = () => {
return <code>{count}</code>;
};
return <Label />;
};