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, useEffect } from 'react' | |
function Example() { | |
const [width, setWidth] = useState(...) | |
useEffect(() => { | |
function handleResize(event) { | |
// Calculate window width... | |
setWidth(...) | |
} |
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, useEffect } from 'react' | |
function FriendStatus({ friendId }) { | |
const [isOnline, setIsOnline] = useState(null) | |
useEffect(() => { | |
function handleStatusChange(status) { | |
setIsOnline(status.isOnline) | |
} |
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, useEffect } from 'react' | |
function Example() { | |
const [width, setWidth] = useState(...) | |
useEffect(() => { | |
function handleResize(event) { | |
// Calculate window width... | |
setWidth(...) | |
} |
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, useEffect } from 'react' | |
function Example() { | |
const [count, setCount] = useState(0) | |
useEffect(() => { | |
document.title = `You clicked ${count} times` | |
}, [count]) // Pass [count] as dependencies | |
return ( |
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, useEffect } from 'react' | |
function FriendStatus({ friendId }) { | |
const [isOnline, setIsOnline] = useState(null) | |
useEffect(() => { | |
function handleStatusChange(status) { | |
setIsOnline(status.isOnline) | |
} |
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, useEffect } from 'react' | |
function Example() { | |
// Read initial count from localStorage | |
const [count, setCount] = useState(localStorage.getItem('count') || 0) | |
// Similar to componentDidMount and componentDidUpdate: | |
useEffect(() => { | |
// Update the document title using the browser API | |
document.title = `You clicked ${count} times` |
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, useEffect } from 'react' | |
function Example() { | |
// Read initial count from localStorage | |
const [count, setCount] = useState(localStorage.getItem('count') || 0) | |
// Similar to componentDidMount and componentDidUpdate: | |
useEffect(() => { | |
// Update the document title using the browser API | |
document.title = `You clicked ${count} times` |
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, useEffect } from 'react' | |
function Example() { | |
const [count, setCount] = useState(0) | |
// Similar to componentDidMount and componentDidUpdate: | |
useEffect(() => { | |
// Update the document title using the browser API | |
document.title = `You clicked ${count} times` | |
}) |
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 { useEffect, useRef, useState } from 'react' | |
export default function useNow(interval = 1000) { | |
const [date, setDate] = useState(() => (new Date())) | |
const intervalRef = useRef() | |
useEffect(() => { | |
intervalRef.current = setInterval(() => { | |
setDate(new Date()) |
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 { useEffect } from 'react' | |
export default function useOnClickOutside(ref, callback) { | |
useEffect(() => { | |
const listener = (event) => { | |
if (ref && ref.current && !ref.current.contains(event.target)) { | |
callback(event) | |
} | |
} |
NewerOlder