Skip to main content

Posts

Showing posts from April, 2020

test hook

https://github.com/testing-library/react-hooks-testing-library useCounter.js import { useState , useCallback } from ' react ' function useCounter () { const [ count , setCount ] = useState ( 0 ) const increment = useCallback (() => setCount (( x ) => x + 1 ), []) return { count, increment } } export default useCounter useCounter.test.js import { renderHook , act } from ' @testing-library/react-hooks ' import useCounter from ' ./useCounter ' test ( ' should increment counter ' , () => { const { result } = renderHook (() => useCounter ()) act (() => { result . current . increment () }) expect ( result . current . count ). toBe ( 1 ) })

custom hook - The Facade pattern

https://wanago.io/2019/12/09/javascript-design-patterns-facade-react-hooks/ function useUsersManagement ( ) {    const [ users , setUsers ] = useState ( [ ] ) ;      function addUser ( user ) {      setUsers ( [        . . . users ,        user      ] )    }      function deleteUser ( userId ) {      const userIndex = users . findIndex ( user = > user . id === userId ) ;      if ( userIndex > - 1 ) {        const newUsers = [ . . . users ] ;        newUsers . splice ( userIndex , 1 ) ;        setUsers (          newUsers        ) ;      }    }      return {      users ,      addUser ,      deleteUser    } }

Graphql Scalars

https://github.com/Urigo/graphql-scalars scalar DateTime scalar EmailAddress scalar NegativeFloat scalar NegativeInt scalar NonNegativeFloat scalar NonNegativeInt scalar NonPositiveFloat scalar NonPositiveInt scalar PhoneNumber scalar PositiveFloat scalar PositiveInt scalar PostalCode scalar UnsignedFloat scalar UnsignedInt scalar URL scalar ObjectID scalar BigInt scalar Long scalar GUID scalar HexColorCode scalar HSL scalar HSLA scalar IPv4 scalar IPv6 scalar ISBN scalar MAC scalar Port scalar RGB scalar RGBA scalar USCurrency scalar JSON scalar JSONObject

iterate array - sum / contact with reduce

http://learnjsdata.com/iterate_data.html var landSum = data.reduce( (sum, d)=> sum + d.land_area, 0 ); console .log(landSum); var weirdString = data.reduce( function (str, d, i) { var ending = (i % 2 === 0 ) ? " is cool." : " sucks." ; return str.concat( " " , d.city , ending) }, "" ); console .log(weirdString);