log/Memo

React 이벤트 위임, RORO 패턴

ycs1m1yk 2022. 8. 26. 02:50

1. React에서 이벤트 위임을 해준다.

React issue #13635

<17 : React doesn't attach your click event handlers to the nodes.
It uses event delegation and listens at the document level.

React 17.0.0 CHANGELOG

>=17 : Delegate events to roots instead of document.


2. RORO(Receive an object, return an object) 패턴

링크

사용하는 이유

  • Named parameters
  • Cleaner default parameters
  • Richer return values
  • Easy function composition

예시

/**
* Named Parameters
*/

// 1. function receive three parameters
function findUsersByRole (  role,   withContactInfo,   includeInactive) {...}
// call
findUsersByRole(  'admin',   true,   true)


// 2. function receive a single object
function findUsersByRole ({ role, withContactInfo, includeInactive }) {...}
// call
findUsersByRole({ role: 'admin', withContactInfo: true, includeInactive: true })
// call with re-ordered parameters
findUsersByRole({ withContactInfo: true, role: 'admin', includeInactive: true })
// call with omitted parameters
findUsersByRole({ role: 'admin', includeInactive: true })