top | item 43721246

(no title)

disillusionist | 10 months ago

you can do this rather easily by returning an object rather than a primitive. if you're using a language like TypeScript, destructuring the resulting returned object is rather trivial and (in my opinion) delightful to read. eg

  function combineNames({ first, last }) {
    const fullName = `${first} ${last}`;
    return { fullName };
  }
  const { fullName } = combineNames({first: 'John', last: 'Doe' });

discuss

order

vbezhenar|10 months ago

For some reason React prefers to return arrays. I never understood the reason.

  const [state, setState] = useState(initialState)
instead of

  const {state, setState} = useState(initialState)