This file contains hidden or 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 * as React from 'react'; | |
import useFriendStatus from './useFriendStatus'; | |
interface IUser { | |
id: number; | |
username: string; | |
} | |
const FriendsListItem ({ user }) => { | |
// We know this value is a boolean since we defined our hook |
This file contains hidden or 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'; | |
type Hook = (friendID: number) => boolean | |
interface IStatus { | |
id: number; | |
isOnline: boolean; | |
} | |
const useFriendStatus: Hook = (friendID) => { |
This file contains hidden or 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
function useFriendStatus(friendID) { | |
const [isOnline, setIsOnline] = useState(null); | |
// ... | |
// Show a label in DevTools next to this Hook | |
// e.g. "FriendStatus: Online" | |
useDebugValue(isOnline ? 'Online' : 'Offline'); | |
return isOnline; |
This file contains hidden or 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
useDebugValue(value) |
This file contains hidden or 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
function useLayoutEffect(effect: EffectCallback, deps?: DependencyList): void; |
This file contains hidden or 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
function useImperativeHandle<T, R extends T>(ref: Ref<T>|undefined, init: () => R, deps?: DependencyList): void; |
This file contains hidden or 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
function FancyInput(props, ref) { | |
const inputRef = useRef(); | |
useImperativeHandle(ref, () => ({ | |
focus: () => { | |
inputRef.current.focus(); | |
} | |
})); | |
return <input ref={inputRef} ... />; | |
} | |
FancyInput = React.forwardRef(FancyInput); |
This file contains hidden or 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
useImperativeHandle(ref, createHandle, [inputs]) |
This file contains hidden or 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
function useRef<T>(initialValue: T): MutableRefObject<T>; | |
interface MutableRefObject<T> { | |
current: T; | |
} |
This file contains hidden or 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
function TextInputWithFocusButton() { | |
// The type of our ref is an input element | |
const inputEl = useRef<HTMLInputElement>(null); | |
const onButtonClick = () => { | |
// `current` points to the mounted text input element | |
inputEl.current.focus(); | |
}; | |
return ( | |
<> |