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
let completedAll = true; | |
for (let i = 0; i < todos.length; i++) { | |
if (!todos[i].completed) { | |
completedAll = false; | |
break; | |
} | |
} |
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 completedAll = todos.every((todo) => todo.completed); | |
console.log(completedAll); // false |
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 todo = todos.find((todo) => todo.id === 1); |
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
let todo: Todo | undefined; | |
for (let i = 0; i < todos.length; i++) { | |
if (todos[i].id === 1) { | |
todo = todos[i]; | |
break; // Remember to break so we don't keep iterating after we found it | |
} | |
} |
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
import React from 'react'; | |
type Todo = { | |
id: number; | |
title: string; | |
completed: boolean; | |
} | |
type Props = { | |
todo: Todo; |
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
type Todo = { | |
id: number; | |
title: string; | |
completed: boolean; | |
} | |
const todos: Todo[] = [ | |
{ id: 1, title: 'Buy nachos', completed: false }, | |
{ id: 2, title: 'Buy avocado', completed: true }, | |
{ id: 3, title: 'Buy ground beef', completed: false }, |
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
type Todo = { | |
id: number; | |
title: string; | |
completed: boolean; | |
} | |
const todos: Todo[] = [ | |
{ id: 1, title: 'Buy nachos', completed: false }, | |
{ id: 2, title: 'Buy avocado', completed: true }, | |
{ id: 3, title: 'Buy ground beef', completed: false }, |
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
import React, { useState, useCallback } from 'react'; | |
import useInterval from './useInterval'; | |
const ShoppingList = () => { | |
// Wait 5 seconds before fetching new data | |
const POLL_DELAY = 5000; | |
const [items, setItems] = useState([]); | |
const fetchItems = useCallback(async () => { | |
const response = await fetch('/shopping-list/items'); |
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
import React, { useEffect, useRef } from 'react'; | |
const useInterval = (callback: () => void, delay: number | null) => { | |
const savedCallback = useRef(callback); | |
useEffect(() => { | |
savedCallback.current = callback; | |
}, [callback]); | |
useEffect(() => { |
NewerOlder