Skip to content

Instantly share code, notes, and snippets.

View ilxanlar's full-sized avatar
🍵
Fresh tea!

Mehdi Namvar ilxanlar

🍵
Fresh tea!
View GitHub Profile
import React, { useState, useEffect } from 'react'
function Example() {
const [width, setWidth] = useState(...)
useEffect(() => {
function handleResize(event) {
// Calculate window width...
setWidth(...)
}
import React, { useState, useEffect } from 'react'
function FriendStatus({ friendId }) {
const [isOnline, setIsOnline] = useState(null)
useEffect(() => {
function handleStatusChange(status) {
setIsOnline(status.isOnline)
}
import React, { useState, useEffect } from 'react'
function Example() {
const [width, setWidth] = useState(...)
useEffect(() => {
function handleResize(event) {
// Calculate window width...
setWidth(...)
}
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 (
import React, { useState, useEffect } from 'react'
function FriendStatus({ friendId }) {
const [isOnline, setIsOnline] = useState(null)
useEffect(() => {
function handleStatusChange(status) {
setIsOnline(status.isOnline)
}
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`
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`
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`
})
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())
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)
}
}