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 { useState, useEffect, useCallback, useRef } from 'react'; | |
| function useIsTextOverflow(value) { | |
| const [isOverflow, setIsOverflow] = useState(false); | |
| const [elementRef, setElementRef] = useState(null); | |
| const [elementSize, setElementSize] = useState(0); | |
| const observerRef = useRef(null); | |
| const refCallback = useCallback(node => { | |
| if (node) { |
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 useStateWithCallback(initialValue) { | |
| const [state, setState] = useState(initialValue); | |
| // we need to track down last changes to support synchronous updates | |
| // e.g, addEventListener handlers | |
| const lastStateRef = useRef(initialValue); | |
| // we need this flag for 2 reasons: | |
| // 1. To prevent the call on mount (first useEffect call) | |
| // 2. To force the effect to run when the state wasn't really updated |
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
| Player.prototype.setScore = function(newScore){ | |
| this.score = newScore; | |
| } |
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
| player1.setScore(1000); | |
| player2.setScore(2000); |
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 createPlayer(userName, score) { | |
| const newPlayer = { | |
| userName, | |
| score, | |
| setScore(newScore) { | |
| newPlayer.score = newScore; | |
| } | |
| } | |
| return newPlayer; | |
| } |
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 Player(userName, score) { | |
| this.userName = userName; | |
| this.score = score; | |
| } | |
| Player.prototype.setScore = function(newScore) { | |
| this.score = newScore; | |
| } |
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
| const player1 = Object.create(null) | |
| player1.userName = 'sag1v'; | |
| player1['score'] = 700; | |
| player1.setScore = function(newScore) { | |
| player1.score = newScore; | |
| } |
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
| Player.call(this, userName, score) |
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
| double.toString() |
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
| { | |
| userName: 'sag1v', | |
| score: 700, | |
| setScore: ƒ | |
| } | |
| { | |
| userName: 'sarah', | |
| score: 900, | |
| setScore: ƒ |
NewerOlder